Java Instance Initializer Block

When we have a code that is located between curly braces ({}), it is called a code block. We often have code blocks within a method, and that part of the code gets called when we invoke the method. Also, we can often see a code block outside the method in the body of a class. These are called Initializer blocks.

There are 2 types of Initializer blocks:

  1. Instance Initializer block,
  2. Static Initializer block.

Instance Initializer block

Look at this example: 

public static void main(String[] args) {
   
  { 
     System.out.println("Summer"); 
  }
}
   
  { 
     System.out.println("Winter"); 
  }

How many code blocks do we have here?

The answer is 3.

You can easily count the blocks by counting the number of pairs of braces.

If we don’t have the same number of open ({) and close (}) braces, we’ll get a compiler error.

As you can see, only one pair of braces is outside the method, which is the Instance Initializer block.

When we have the code that initializes instance variables in several places within a class, we need to keep track of the initialization order.

Things to remember:

  1. Instance initializer blocks run in the order they appear in the file.
  2. The instance initializer block gets invoked after the parent class constructor is invoked.
  3. The instance initializer runs each time an object of the class is created.


Example 1:

 1  public class Car {
 2    private String model = "Ford";
 3    
 4    { 
 5      System.out.println("Instance Initializer block..."); 
 6    }
 7    
 8    public Car () {
 9      model = "Opel";
10      System.out.println("Constructor...");
11    }
12  
13    public static void main(String[] args) {
14      Car car = new Car();
15      System.out.println(car.model); 
16    } 
17  }
Output: Instance Initializer block… Constructor… Opel
 
Let’s look at the order of execution:

On line 14, we call Car’s constructor and create a new object. First, it initializes the brand to “Ford” on line 2. Next, it executes the print statement in the instance initializer on line 5. Once all the fields and instance initializers have run, Java returns to the constructor.

Line 9 changes the brand’s value to “Opel” and line 10 prints another statement. At this point, the constructor is done executing and goes back to the print statement on line 15.


Example 2:

class Car {  
  
  Car() {
    System.out.println("Constructor...");
  }  
   
  {
    System.out.println("Instance Initializer block...");
  }  
       
  public static void main(String args[]) {  
    Car car1 = new Car();  
    Car car2 = new Car();  
  }      
}
Output: Instance Initializer block… Constructor… Instance Initializer block… Constructor…

When we look at the output, it seems that instances of the Instance initializer blocks were executed first in both cases. But in reality, that’s not the case.

The constructors get invoked first.

Behind the scenes, Java copies instance initializer blocks into constructors immediately after the first line, which holds the super keyword (a call to the super-class constructor).

See the following example (number 3) to better understand.

Example 3:

class Vehicle {
  
  Vehicle() {
    System.out.println("The constructor of the Vehicle class...");
  }
}

class Car extends Vehicle {  

  {
    System.out.println("Instance Initializer block...");
  }  
  
  Car() {
    System.out.println("The constructor of the Car class...");
  }      
} 

class Test {
  
  public static void main(String args[]) {  
    Car car = new Car();  
  }  
}
Output: The constructor of the Vehicle class… Instance Initializer block… The constructor of the Car class…

Static Initialization Block

We’ll cover the Static Initialization Block in the next lesson.

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *