Instance Initializer block in Java

Apart from methods and constructors, Initialization Blocks are the third place in a Java Program where operations can be performed. Initialization Blocks come in two flavors:

1.Static Initialization Blocks: Runs first when the class is first loaded. Declared by using the keyword Static.

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

 class InitDemo

{
	static int y;
	int x;
	// Static Initialization Block
	static {
		y = 10;
	}
	// Instance Initialization Block
	{
		x = 20;
	}
}

Instance initialization block code runs right after the call to super() in a constructor, in other words, after all super constructors have run. The order in which initialization blocks appear in a class matters. If a class has more than one they all run in the order they appear in the class file.

Some rules to remember:

  • Initialization blocks execute in the order they appear.
  • Static Initialization blocks run once when the class is first loaded.
  • Instance Initialization blocks run every time a class instance is created.
  • Instance Initialization blocks run after the constructor call to super().

Instance initializer block:

Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created. The initialization of the instance variable can be directly but there can be performed extra operations while initializing the instance variable in the instance initializer block.

Why use instance initializer block?

Suppose I have to perform some operations while assigning value to instance data member e.g. a for loop to fill a complex array or error handling etc.

1.//Program of instance initializer block that initializes values to the instance variable

 class Bike {
	int speed;

	Bike() {
		System.out.println("speed is " + speed);
	}

	{
		speed = 100;
	}

	public static void main(String args[]) {
		Bike b1 = new Bike();
		Bike b2 = new Bike();
	}
}
Output:speed is 100
       speed is 100
 

There are three places in java where you can perform operations:

1.Method

2.Constructor

3.Block

Flow of invoked of instance initializer block & constructor

			 
class Bike {
	int speed;

	Bike() {
		System.out.println("constructor is invoked");
	}

	{
		System.out.println("instance initializer block invoked");
	}

	public static void main(String args[]) {
		Bike b1 = new Bike();
		Bike b2 = new Bike();
	}
}
Output:instance initializer block invoked
       constructor is invoked
       instance initializer block invoked
       constructor is invoked
       

Rules for instance initializer block :

There are mainly three rules for the instance initializer block. They are as follows:

1. The instance initializer block is created when instance of the class is created.

2. The instance initializer block is invoked after the parent class constructor is invoked (i.e. after super() constructor call).

3. The instance initializer block comes in the order in which they appear.

class A {
	A() {
		System.out.println("parent class constructor invoked");
	}
}

class B2 extends A {
	B2() {
		super();
		System.out.println("child class constructor invoked");
	}

	{
		System.out.println("instance initializer block is invoked");
	}

	public static void main(String args[]) {
		B2 b = new B2();
	}
}

Another example of instance block

 class A {
	A() {
		System.out.println("parent class constructor invoked");
	}
}

class B3 extends A {
	B3() {
		super();
		System.out.println("child class constructor invoked");
	}

	B3(int a) {
		super();
		System.out.println("child class constructor invoked " + a);
	}

	{
		System.out.println("instance initializer block is invoked");
	}

	public static void main(String args[]) {
		B3 b1 = new B3();
		B3 b2 = new B3(10);
	}
}


Output:parent class constructor invoked
       instance initializer block is invoked
       child class constructor invoked
       parent class constructor invoked
       instance initializer block is invoked
       child class constructor invoked 10