In what order do static blocks and initialization blocks execute when using inheritance?

JavaInheritanceConstructorStatic Block

Java Problem Overview


I have two classes Parent and Child

public class Parent {    
	public Parent() {
		System.out.println("Parent Constructor");
	}    
	static {
		System.out.println("Parent static block");    
	}    
	{
		System.out.println("Parent initialisation  block");
	}
}
    
public class Child extends Parent {    
	{
		System.out.println("Child initialisation block");
	}
	static {
		System.out.println("Child static block");
	}

	public Child() {
		System.out.println("Child Constructor");
	}    
	public static void main(String[] args) {
		new Child();    
	}
}

The output of the above code will be

Parent static block
Child static block
Parent initialization  block
Parent Constructor
Child initialization block
Child Constructor

Why does Java execute the code in that order? What are the rules that determine the execution order?

Java Solutions


Solution 1 - Java

I learn visually, so here's a visual representation of order, as a SSCCE:

public class Example {

    static {
        step(1);
    }

    public static int step_2 = step(2);
    public int step_8 = step(8);

    public Example(int unused) {
        super();
        step(10);
    }

    {
        step(9);
    }

    // Just for demonstration purposes:
    public static int step(int step) {
        System.out.println("Step " + step);
        return step;
    }
}
public class ExampleSubclass extends Example {

    {
        step(11);
    }

    public static int step_3 = step(3);
    public int step_12 = step(12);

    static {
        step(4);
    }

    public ExampleSubclass(int unused) {
        super(step(7));
        step(13);
    }

    public static void main(String[] args) {
        step(5);
        new ExampleSubclass(step(6));
        step(14);
    }
}

This prints:

Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12
Step 13
Step 14

Keep in mind that the order of the static parts matters; look back at the difference between the order of Example's static stuff and ExampleSubclass's.

Also note that the instance initialization block is always executed immediately after the super() call in the constructor (even if that call is implied/omitted), no matter the order. However, order does matter between an initialization block and a field initializer.

Solution 2 - Java

There are several rules in play

  • static blocks are always run before the object is created, so that's why you see print messages from both parents and child static blocks
  • now, when you are calling constructor of the subclass (child), then this constructor implicitly calls super(); before executing it's own constructor. Initialization block comes into play even before the constructor call, so that's why it is called first. So now your parent is created and the program can continue creating child class which will undergo the same process.

> Explanations: > > 1. Static block of parent is executed first because it is loaded first and static blocks are called when the class is loaded.

Solution 3 - Java

First - run child class only (comment the extend clause) to see the simple flow.

second - go to https://stackoverflow.com/questions/12550135/static-block-vs-initializer-block-in-java & read the accepted answer over there.

Edit:

  1. Execution happens in SIC way - Static, (non static) Initializer & Constructor.
  2. (Non static) Initializer are copied into every constructor - At the TOP! (hence lines 3/4/5/6)
  3. Before a class is initialized, its direct superclass must be initialized - http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4 (hence parent static block appears first).

Solution 4 - Java

  • Static init blocks are executed at the time of class loading.
  • In the class hierarchy the order for execution of static init blocks will start from top level class.
  • In a class the order for the execution of static block is from top to bottom.
  • Above rule apply regardless of where the static block is present within the class.

(In your code the parent static blocks will be executed first and then the child class static blocks.)

  • Instance init blocks will be executed after the call to the super(); in the constructor.
  • Always super(); is the very first statement in a default constructor.

In your code when you create a Child object:

  • The default constructor of the Child class get executed.
  • It will call to the super(); constructor.
  • Then the super class constructor is executed.
  • The Parent class will execute its super(); call.
  • After that the instance init blocks in the Parent class are executed.(From top to bottom).
  • Then the code within the constructor is executed (if any).
  • Then it will return to the Child class and execute the Child class instance init blocks.
  • Finally the code in the child constructor get executed (If exists).

Solution 5 - Java

Static block in java is executed before main method. If we declare a Static block in java class it is executed when class loads. This is initialize with the static variables. It is mostly used in JDBC. Static block in java is executed every time when a class loads. This is also known as Static initialization block. Static block in java initializes when class load into memory , it means when JVM read the byte code. Initialization can be anything; it can be variable initialization or anything else which should be shared by all objects of that class. Static block is a normal block of code enclosed in braces { } and is preceded by static keyword.

so static block executed first.

Instance Initialization Blocks: Runs every time when the instance of the class is created.

so next Initialization block executed when instance of the class is created.

then Constructor executed

Solution 6 - Java

Just wanted to share my findings. I read in one of the answers on another thread that static blocks are executed first before static fields which is not correct. It depends on which comes first, static field or static block. Have a look at below code. It will try to put things in perspective.

  1. JVM looks for a class which has public static void main(String args[]) so that it can load that class.

  2. It then initialises static fields of this class(if they come before static blocks). These fields can call static methods of this class or another. If they call static method of this class then that method gets served. If they call static method of another class, then static fields or blocks of that class(depending on which comes first) gets initialised first, then this method call is served.

  3. Then, it moves to static blocks.

  4. It comes back to main method.

    class TestLab {
    static int method(String a) {
    	System.out.println("in static method of TestLab" + " Coming from " + a);
    	System.out.println("b is " + b);
    	return 6;
    }
    
    static int a = method("Line 11");
    static int b = 7;
    
    TestLab() {
    	System.out.println("Inside test lab constructor");
    }
    
    static {
    	System.out.println("In static block of TestLab");
    }
    
    }
    
    public class Test1 {
    public static void main(String[] args) {
    	System.out.println("inside main method of Test 1");
    	int a = TestLab.method("Line 26");
    }
    
    // static Test ref=new Test();
    Test1() {
    	System.out.println("Default Constructor of Test1");
    }
    
    {
    	System.out.println("In instance block of Test1");
    }
    static int d = TestLab.method("Line 37");
    static int e = methodOfTest1();
    static {
    	System.out.println("In Static Block of Test1");
    }
    
    static int methodOfTest1() {
    	System.out.println("inside static method:mehtodOfTest1()");
    	return 3;
    }
    }
    

Here is the output:

in static method of TestLab Coming from Line 11
b is 0
In static block of TestLab
in static method of TestLab Coming from Line 37
b is 7
inside static method:mehtodOfTest1()
In Static Block of Test1
inside main method of Test 1
in static method of TestLab Coming from Line 26
b is 7

Solution 7 - Java

It would be very helpful to ckeck out the object construction process with a step by step debuger, having a view in which you can see how your object is goning through the phases. I found this very useful for clearing the perspective from a higher point of view. Eclipse can help you with this with it's debugger step into function.

Solution 8 - Java

Here is what I found while preparing for a certification.

While we run a class, first static blocks/ static variable initialisation happens. If multiple static blocks are there, it will execute it in the order in which it appears,

Then it will execute init blocks/ instance variable initialisation.If multiple init blocks/ variable initialisation are there, it will execute it in the order in which it appears,

Afterwards it will look into the constructor.

Solution 9 - Java

Static block gets executed when a class is loaded into JVM. While init block gets copied into the Constructor whose object will be created and runs before creation of object.

Solution 10 - Java

control flow is-

static block -> Initialization block -> and finally Constructor.

static block -> This static block will be get executed only once when the control come to the class.(JVM Load this class)

Initialization block -> This Initialization block will be get executed whenever a new object Created for the Class (It will be executed from second statement of the Constructor then following constructor statements- remember First statement of the Constructor will be Super()/this())

Constructor -> This will be get whenever a new object is created.

Solution 11 - Java

  1. Static init block executes at the time of class loading only ones.
  2. Init block executes every time before creating object of the class.

Learn more with this video: Java Tutorial by Rajan Jain : Static Init Block and Init Block in java Video14 - YouTube

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCKR666View Question on Stackoverflow
Solution 1 - JavaKy.View Answer on Stackoverflow
Solution 2 - JavaPetr MensikView Answer on Stackoverflow
Solution 3 - JavaRaúlView Answer on Stackoverflow
Solution 4 - JavaSayanthan MahendranView Answer on Stackoverflow
Solution 5 - Javarohan kamatView Answer on Stackoverflow
Solution 6 - JavapragunView Answer on Stackoverflow
Solution 7 - JavaMihai SavinView Answer on Stackoverflow
Solution 8 - JavaPRAVEEN PSView Answer on Stackoverflow
Solution 9 - JavaSunil Kumar JhaView Answer on Stackoverflow
Solution 10 - JavaPrakash VLView Answer on Stackoverflow
Solution 11 - JavaJava TutorialView Answer on Stackoverflow