Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:03:09+00:00 2026-05-13T10:03:09+00:00

class test { test() { System.out.println("Constructor"); } { System.out.println("Hai"); } } public class sample

  • 0
class test
{
    test() {
        System.out.println("Constructor");
    }

    { System.out.println("Hai"); }
}


public class sample
{
    public static void main(String [] a) {
        test t = new test();        
    }
}

In the above code, why is "Hai" printed before the test() constructor is called?

The test() constructor in the test class is above the "Hai" statement and should be called first, right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:03:09+00:00Added an answer on May 13, 2026 at 10:03 am

    Let express with a more clear example:

    public class Test {
    
        static {
             System.out.println("static initializer");
        }
    
        {
             System.out.println("instance initializer");
        }
    
        public Test() {
             System.out.println("constructor");
        }
    
    }
    

    and test it as follows:

    public class Main {
    
        public static void main(String[] args) {
            Test test1 = new Test();
            Test test2 = new Test();
        }
    
    }
    

    output:

    static initializer
    instance initializer
    constructor
    instance initializer
    constructor
    

    The static initializers are executed only once during runtime, specifically during loading of the class. The instance initializers are executed during every instantiation before the constructor.

    You can have more than one of them and they will be executed in the order as they appear in the coding.

    The major benefit of instance initializers is that they are executed regardless of which constructor you use. They applies on each of them so that you don’t need to duplicate common initialization over all of them.

    The major benefit of static initializers is that they are executed only once during class loading. A well known real world example is the JDBC driver. When you do

     Class.forName("com.example.jdbc.Driver");
    

    which only executes the static initializers, then any (decent) JDBC driver will register itself in the DriverManager as follows

     static {
          DriverManager.registerDriver(new com.example.jdbc.Driver());
     }
    

    this way the DriverManager can find the right JDBC driver during getConnection().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test{ public static void main(String[] arg){ System.out.println(Alexander The Great); } } In
class Test{ public static void main(String... s){ System.out.println(Hello); } } class Test{ public static
public class RunScript { public static void main(String[] args) { System.out.println(Hello World); } }
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
class test { public static void main(String args[]) { test ob=new test(); String st=new
public class Test { public static void main(String[] args) { int[] arr = new
MyClass.java: package test; public class MyClass { public void myMethod(){ System.out.println(My Method Called); }
using System; using System.Math; class test { public static void Main() { Console.Write(Enter any
class Test { public static void main(String[] args) throws Exception { Test t =
public class Test { public static void main(String[] args){ if (5.0 > 5) //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.