Master java skills

Instance initializer Block

Instance initializer block is a block of code that is used to initialize instances/objects of a class.

But wait a min, isn’t the same thing done in the constructors as well? So, what is the need of this instance initializer block?

Well, it still has its own utility. Suppose, we have multiple constructors within a class, and we need to have some object initialization code which goes in all the constructors. Don’t you think, instead of writing the same common code in all the constructors, it would have been better if we could write it in a common place. There comes instance initializer blocks for our rescue.

When is instance initializer block is called

Instance initializer block is called every time an object of the class is created. It is called just after the super call in the constructor and just before any other statement of the constructor.

Note -> instance initializer block is called between super call and other statements of the constructor. It is called for every constructor call regardless of whether it is a default or parameterized constructor.

How to define an instance initializer block

{
    //some code here
    //some more code
}
package com.javatrainingschool;

public class ITCompany {
	
	private String name;
	
        //instance initializer block
	{
		System.out.println("IT company initialized.");
	}
	
	public ITCompany() {
		super();
		System.out.println("From inside default constructor");
	}

	public ITCompany(String name) {
		super();
		this.name = name;
		System.out.println("From inside one argument constructor");
	}

	public static void main(String[] args) {
		ITCompany comp = new ITCompany();
		ITCompany comp1 = new ITCompany("Java Training School");
	}
}
Output :

IT company initialized.
From inside default constructor
IT company initialized.
From inside one argument constructor

In the above example, we can see that instance initializer block is invoked every time an object is created.

Some important points about instance initializer block

  1. Instance initializer block is called every time an object of the class is created.
  2. It is invoked after the parent class constructor is called (call to super()) but before any statement of the this class constructor.
  3. If there are more than one instance initializer block in the class, they are invoked in the order in which they are declared in the class.

Example of multiple instance initializer blocks

package com.javatrainingschool;

public class ITCompany {
	
	private String name;
	
        // first instance initializer block
	{
		System.out.println("First initializer block.");
	}

        // second instance initializer block
	{
		System.out.println("Second initializer block.");   
	}
	
	public ITCompany() {
		super();
		System.out.println("From inside default constructor");
	}

	public ITCompany(String name) {
		super();
		this.name = name;
		System.out.println("From inside one argument constructor");
	}

	public static void main(String[] args) {
		ITCompany comp = new ITCompany();
		ITCompany comp1 = new ITCompany("Java Training School");
	}
}
Output :

First initializer block.
Second initializer block.
From inside default constructor
First initializer block.
Second initializer block.
From inside one argument constructor