Search code examples
javastaticstatic-methods

Could anyone explain the reason why output is printed in this order for both the codes(Static keyword execution priority)


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


Solution

  • 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