Java – Constructors, Initialization blocks and their execution order

In earlier articles, we have learnt about Java Constructor and Initialization blocks in detail. But we learnt all these concepts individually and what if both constructor and initialization blocks present at the same time

Basically, we will look into execution order of

If all present in one class

Quick points about this topic:

  • Constructor gets executed, only when object is created using new keyword
  • Instance blocks gets executed, only when object is created using new keyword
  • Static blocks gets executed, at the time of class loading

Keeping all these key points in mind, we will design a simple Java class to illustrate an example on execution order of Constructors and Initialization blocks

Below class contains

  • 1 default constructor
  • 2 static blocks
  • 2 instance blocks
  • 1 instance method
  • 1 main() method – entry point to JVM to begin execution

TestExecutionOrder.java

package in.bench.resources.initialization.blocks.example;

public class TestExecutionOrder {

	// instance block 1 -
	{
		System.out.println("instance block 1 - before default constructor");
	}

	// default constructor
	TestExecutionOrder(){
		System.out.println("default constructor of TestExecutionOrder class");
	}

	// static block 1 -
	static {
		System.out.println("static block 1 - after default constructor");
	}

	// instance block 2 -
	{
		System.out.println("instance block 2 - in between static block 1 & 2");
	}

	// static block 2 -
	static {
		System.out.println("static block 2 - after instance block 2");
	}

	// display method to print message to console
	void displayMethod() {
		System.out.println("simple display() method to print message and read execution order");
	}

	// main() method - entry point to JVM
	public static void main(String[] args) {
		System.out.println("main() method - entry point to JVM");
		TestExecutionOrder teo = new TestExecutionOrder();
		teo.displayMethod(); // invoking displayMethod();
	}
}

Output:

static block 1 - after default constructor
static block 2 - after instance block 2
main() method - entry point to JVM
instance block 1 - before default constructor
instance block 2 - in between static block 1 & 2
default constructor of TestExecutionOrder class
simple display() method to print message and read execution order

Explanation:

In above TestExecutionOrder class,

  • When we execute above class, TestExecutionOrder class is loaded into JVM
  • First thing it does is, executing all static blocks i.e.; static block 1 & 2 gets executed even before main() method gets executed
  • Since main() method is static, next this will get executed and simple entry message gets printed in the console
  • Next line in main() method is the creation of object using new keyword which invokes default no-arg constructor and thus its get executed
  • But due to the presence of implicit super() constructor class, super class’s constructor gets executed
  • Again when control comes back to constructor, all instance blocks gets executed in the order they are defined before actual constructor statement execution
  • Finally constructor statements gets executed and a new object is created
  • Now, using newly created object we can invoke instance methods likewise we have invoked displayMethod() and printing simple messages in the console

Note: Static methods, if present are loaded into JVM at the time of class loading. Since static methods are class methods, it can be invoked directly using classname and there is no need of object creation steps here

Related Articles:

Happy Coding !!
Happy Learning !!

Java - Static Initialization blocks v/s Instance Initialization blocks
Java - Initialization blocks with examples