Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created.

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

There are two type of Initialization block :
1) Static Block
2) Instance Initialization Blocks

In this page we will study about Instance Initialization Blocks .

Instance Initialization Blocks : It is block which runs every time when the object of the class is created.

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

  • Simple Example of Instance initializer block

    In this example we create simple instance block in Test class.

    Example)
    drag_handle
     class Test {	
    	{
    		System.out.println("This is instance block");
    	}
    
    	public static void main(String[] args) {
    		Test t = new Test();// creating object
    		Test t2 = new Test();// creating another object
    	}
    }
    Output:
    This is instance block
    This is instance block
    
  • Why use 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.

    To perform extra operation while initializing the data member , we use instance initializer block.

  • Order of Execution of Instance Initializer block.

    Order is from top to bottom. lets see the example: In this example we create three instance initializer block.

    Example)
    drag_handle
     class Test {
    	{
    		System.out.println("This is First instance block");
    	}
    
    	{
    		System.out.println("This is Second instance block");
    	}
    
    	{
    		System.out.println("This is Third instance block");
    	}
    
    	public static void main(String[] args) {
    		Test t = new Test();// creating object
    
    	}
    }
    Output:
    This is First instance block
    This is Second instance block
    This is Third instance block
    
  • Rules for instance initializer block :

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

    • The instance initializer block is created when object of the class is created.

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

    • The instance initializer block get executed in the order in which they appear.

  • Change done by java compiler in java program.

    compiler put all the instance initializer blocks inside the constructor after super call.

    Change done by java compiler in java program.
  • Program of instance initializer block that is invoked after super()

    Example)
    drag_handle
     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();
    }
    }
    
    Output:
    parent class constructor invoked
    instance initializer block is invoked
    child class constructor invoked

What next?

The next topic is Inheritance in Java

Share this page