Order of execution of Initialization blocks and Constructors in Java

Prerequisite : Static blocks, Initializer block, Constructor
In a Java program, operations can be performed on methods, constructors and initialization blocks.
Instance Initialization Blocks : IIB are used to initialize instance variables. IIBs are executed before constructors. They run each time when object of the class is created.
Initializer block : contains the code that is always executed whenever an instance is created. It is used to declare/initialize the common part of various constructors of a class.
Constructors : are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.

Order of execution of Initialization blocks and constructor in Java

  1. Static initialization blocks will run whenever the class is loaded first time in JVM
  2. Initialization blocks run in the same order in which they appear in the program.
  3. Instance Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate order of
// execution of constructors, static
// and initialization blocks
class GFG {
  
    GFG(int x)
    {
        System.out.println("ONE argument constructor");
    }
  
    GFG()
    {
        System.out.println("No  argument constructor");
    }
  
    static
    {
        System.out.println("1st static init");
    }
  
    {
        System.out.println("1st instance init");
    }
  
    {
        System.out.println("2nd instance init");
    }
  
    static
    {
        System.out.println("2nd static init");
    }
  
    public static void main(String[] args)
    {
        new GFG();
        new GFG(8);
    }
}

chevron_right


Output



1st static init
2nd static init
1st instance init
2nd instance init
No  argument constructor
1st instance init
2nd instance init
ONE argument constructor

Note : If there are two or more static/initializer blocks then they are executed in the order in which they appear in the source code.

Now, predict the output of the following program-

filter_none

edit
close

play_arrow

link
brightness_4
code

// A tricky Java code to predict the output
// based on order of 
// execution of constructors, static 
// and initialization blocks
class MyTest {
    static
    {
        initialize();
    }
      
    private static int sum;
      
    public static int getSum()
    {
        initialize();
        return sum;
    }
  
    private static boolean initialized = false;
  
    private static void initialize()
    {
        if (!initialized) {
            for (int i = 0; i < 100; i++)
                sum += i;
            initialized = true;
        }
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(MyTest.getSum());
    }
}

chevron_right


Output:

9900

Explanation: