Why the execution order of Constructor and Initialization blocks is pre-defined?

In previous articles, we already discussed about Java Constructor, Initialization blocks (both static & instance) and also their execution order

Read through below article to get hindsight into the topic

 

Execution order of Constructor and Initialization blocks:

Whenever a new instance is instantiated (or created), then their corresponding constructor and instance initialization blocks is executed as per below order;

  • 1st instance initializer blocks get executed as per order defined in the class
  • And then constructor

 

Now, let us focus on the next & main question i.e.; why the execution order of initialization blocks and constructor is pre-defined in Java

 

To understand this, we will execute a small demo program

Person.java

package in.bench.resources.execution.order.of.constructor.init.block;

public class Person {

	// member variable
	int personId;
	String personName;
	int personAge;

	// default no-arg constructor
	public Person() {
		System.out.println("Default no-arg constructor");
	}

	// instance initializer block - 1
	{
		System.out.println("Init-block-1 : Intializing Person Id value to 1001");
		personId = 1001;
	}

	// main() method
	public static void main(String[] args) {

		// instantiating a new object
		Person person1 = new Person();

		// invoking display() method
		person1.display();
	}

	// instance initializer block - 2
	{
		System.out.println("Init-block-2 : Intializing Person Name value to Pup");
		personName = "Pup";
	}

	// instance method to display person info
	public void display() {

		System.out.println("Person Info : " + this.personId + " - " 
				+ this.personName + " - " + this.personAge);
	}

	// instance initializer block - 3
	{
		System.out.println("Init-block-3 : Intializing Person Age value to 35");
		personAge = 35;
	}
}

Output:

Init-block-1 : Intializing Person Id value to 1001
Init-block-2 : Intializing Person Name value to Pup
Init-block-3 : Intializing Person Age value to 35
Default no-arg constructor
Person Info : 1001 - Pup - 35

 

Explanation:

  • From above Person class execution, it is clear that all init-blocks got executed as per order from top-to-bottom and then constructor
  • But all these blocks & constructor gets executed, due to object instantiation step inside main() method
  • Without this step, no init-block or constructor gets executed

 

Now, further why the executions order of all init-blocks executed first and then constructor?

Reason: after compilation, JVM puts or inserts all init-block statements inside constructor at the very beginning as per order (from top-to-bottom)

 

To understand this, we need to de-compile the already compiled Person class (from its .class file)

De-compiled Person class:

/*
 * Decompiled with CFR 0_118.
 */
package in.bench.resources.execution.order.of.constructor.init.block;

import java.io.PrintStream;

public class Person {
	int personId;
	String personName;
	int personAge;

	public Person() {
		System.out.println("Init-block-1 : Intializing Person Id value to 1001");
		this.personId = 1001;
		System.out.println("Init-block-2 : Intializing Person Name value to Pup");
		this.personName = "Pup";
		System.out.println("Init-block-3 : Intializing Person Age value to 35");
		this.personAge = 35;
		System.out.println("Default no-arg constructor");
	}

	public static void main(String[] args) {
		Person person1 = new Person();
		person1.display();
	}

	public void display() {
		System.out.println("Person Info : " + this.personId + " - " 
				+ this.personName + " - " + this.personAge);
	}
}

 

Explanation:

  • If you check the version of de-compiled Person class, then there aren’t any init-block present in the class
  • And all init-block statements are inserted into constructor of the Person class
  • It is inserted in a such a way that, all init-block statements comes first prior to any statements already present inside the constructor

Hope, it helps to understand better why the execution order of constructor & init-blocks is pre-defined

It’s time for you folks to reply back with your comments and suggestion

 

Happy Coding !!
Happy Learning !!