2

CODE 1:

    public class test {
        static {System.out.println("I am here in static");}
        public test()
        {
            System.out.println("I am here in constructor");
        }
    
        public static void main(String[] args)
        {
            System.out.println("I am here in Main");
            test staticCheck=new test();
        }

OUTPUT 1: I am here in static I am here in Main I am here in constructor

CODE 2:

    public class test {
        {System.out.println("I am here in static");}
        public test()
        {
            System.out.println("I am here in constructor");
        }
    
        public static void main(String[] args)
        {
            System.out.println("I am here in Main");
            test staticCheck=new test();
    
        }
    
    }

OUTPUT 2: I am here in Main I am here in static I am here in constructor

  • 1
    Does this answer your question? [In what order do static/instance initializer blocks in Java run?](https://stackoverflow.com/questions/2007666/in-what-order-do-static-instance-initializer-blocks-in-java-run) – Osama A.Rehman Mar 25 '21 at 06:21

2 Answers2

3

Because whoever wrote CODE 2 is a liar.

"I am here in static" in CODE 2 should be "I am here in instance initialization block" instead. IIBs run every time an instance is initialised, before constructors.

See more in this tutorial.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • What did u mean by liar here.When I run these codes in Intellij IDE I got these outputs which I have shared here. – Gowrisankar Mar 25 '21 at 06:30
  • @Gowrisankar he said that because the block is not really static, even though that is what the block printed out to the console. Don't mind that. I am sure it was meant as a joke. – hfontanez Mar 25 '21 at 06:36
  • @hfontanez I didn't take that as serious but was confused by what he meant.Thanks for clearing that – Gowrisankar Mar 25 '21 at 10:33
1

In Code 1, the line static {System.out.println("I am here in static");} is a Static Initialization block (also known as Static Initializer). A static initializer block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. These blocks are used to resolve dependencies a particular class may have BEFORE the constructor is called. Think of them as preconditions. Because static members belong to the class and not the objects (instances), they must be loaded before any instance is created (happens once).

On Code 2, when the keyword static is removed, the block become an Initializer block. Initializer blocks are similar to static initializer, except these must occur in the process of creating class instances. Therefore, they will executed once per every instance created. So, if you were to add another line to the main method to create a second instance of test your output will be:

I am here in Main
I am here in static
I am here in constructor
I am here in static
I am here in constructor

Notice that "I am here in static" and "I am here in constructor" appear twice (once per instance) and the initializer block is executed before the constructor. These initializer blocks are not used very common, but they are useful in cases where you have a sequence of instructions that can be shared by all the constructors of a class. You can see why this is rarely used.

If we were to add back the static keyword, the output of the program with two objects created would be:

I am here in static
I am here in Main
I am here in constructor
I am here in constructor

As I mentioned before, because the initializer now is static it only occurs once.

The order of execution of "I am here in Main" and "I am here in constructor" should require no explanation. But, just in case, because this is the order in which they appear inside the main method: the System.out.println("I am here in Main") occurs before the staticCheck object is instantiated.

UPDATE: This is a little out of scope, but related. If you were to add a parent class to the class shown here, the order of execution will be as follows:

  1. Print out contents of parent class static initializer
  2. Print out contents of this class (child) static initializer
  3. Print out "I am here in Main"
  4. Print out non-static initializer of parent class (if any)
  5. Print out contents of parent constructor
  6. Print out non-static initializer of this (child) class
  7. Print out contents of this class constructor
hfontanez
  • 5,774
  • 2
  • 25
  • 37