Instance Initializer Block In Java

When working with object creation in Java, it is common to initialize variable values. Although values can be assigned directly, additional actions may be necessary during the initialization process. The Instance Initializer Block comes in handy in such cases. In the upcoming section, we will delve into the concept of the Instance Initializer Block and explore its functionality through illustrative examples.

What is Instance Initializer Block in Java?

The Instance Initializer Block is employed to initialize instance variables and executes each time an object of the class is instantiated, enabling additional operations to be carried out during initialization.

Syntax of Instance Initializer Block

A block of code known as an Instance Initializer Block is positioned within a class but not inside any method or constructor. It is enclosed by curly braces { }.

Syntax

Below is the syntax used for initializing instance variables:

Example

{

    // code to initialize instance variables

}

Points to Remember:

Some of the important points regarding instance initializer block are:

  • It is used to initialize instance variables.
  • You can have multiple instance initializer blocks in a class; they execute in the order they appear.
  • It runs before the constructor when an object is created.
  • Example of Instance Initializer Block

Let's consider a straightforward example of an instance initializer block that carries out initialization.

Example

class Bike7{

    int speed;

    

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

 

    {speed=100;}

     

    public static void main(String args[]){

    Bike7 b1=new Bike7();

    Bike7 b2=new Bike7();

    }    

}

Output:

Output

speed is 100

speed is 100

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

  • method
  • constructor
  • block
  • Order of Execution: Instance Initializer Block and Constructor

In Java, upon creating an object, both the constructor and the instance initializer block get executed. Although it seems like the instance initializer block runs initially, in reality, the Java compiler integrates the instance initializer block code into the constructor following the super call. Consequently, the constructor commences execution first, followed by the instance initializer block code execution.

Example

The subsequent illustration showcases the sequence of execution of an instance initializer block and a constructor:

Example

class Bike8 {  

    int speed;  

    

    Bike8() {

        System.out.println("constructor is invoked");

    }  



    {  

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

    }  

    

    public static void main(String args[]) {  

        Bike8 b1 = new Bike8();  

        Bike8 b2 = new Bike8();  

    }      

}

Output:

Output

instance initializer block invoked

constructor is invoked

instance initializer block invoked

constructor is invoked

In the example above, it may appear that the instance initializer block is the first to be executed, but this is not the case. The instance initializer block is actually executed during object creation. When Java compiles the code, it places the instance initializer block in the constructor after the initial statement super. Therefore, the constructor is actually invoked first. This sequence can be better understood by referring to the diagram provided below:

Note: The java compiler copies the code of instance initializer block in every constructor.

Rules for instance initializer block

There are mainly three important rules of the instance initializer block in Java:

  • The instance initializer block is executed whenever an object of the class is created.
  • The instance initializer block is invoked after the parent class constructor (super) is called .
  • If a class has multiple instance initializer blocks, they are executed in the order in which they appear in the class .
  • Instance Initializer Block Executed After super Call

The below code snippet demonstrates that the instance initializer block runs after invoking the parent class constructor (super) and before executing the child class constructor body:

Example

Example

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:

Output

parent class constructor invoked

instance initializer block is invoked

child class constructor invoked

Explanation

  • When an object of the child class is created, the parent class constructor is called first using super.
  • After that, the instance initializer block is executed.
  • Finally, the child class constructor body is executed.
  • Instance Initializer Block with Multiple Constructors

This example illustrates the execution of the instance initializer block for each object creation, irrespective of the constructor being invoked.

Example

Example

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:

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

Explanation

  • Each time an object of class B3 is created, the parent class constructor is invoked first.
  • The instance initializer block is executed next.
  • Finally, the corresponding child class constructor body is executed.
  • The instance initializer block runs for both constructors , showing that it is independent of constructor overloading.

Input Required

This code uses input(). Please provide values below: