Java Instance Initializer Block: A Guide to Instance Initialization

Discover how the instance initializer block in Java enables you to initialize instance data members efficiently. This guide includes an example demonstrating its functionality, helping you understand its differences from constructors and its practical applications in Java programming.



Java - Instance Initializer Block

An instance initializer block is a block of code declared inside a class to initialize the instance data members. It is executed once for each object and can be used to set initial values for instance variables. The instance initializer block is similar to the Java constructor, but its execution and uses are different.

Java Instance Initializer Block Example

This example demonstrates the use of an instance initializer block in Java:

Code Snippet

public class Tester {
public int a;
{ a = 10; }   
}

Characteristics of Instance Initializer Block

  • Called once an object is created.
  • Executed before any constructor of an object is invoked.
  • In a child class, the instance initializer block will be called after the superclass constructor call.
  • Can be used to execute multiple statements.
  • Generally used to instantiate multiple value fields, such as arrays.

Use of Instance Initializer Block

The following are the uses of the instance initializer block in Java:

  • To initialize instance variables.
  • To initialize resources used in the code.
  • To perform dynamic initialization of instance variables.
  • To use common initialization logic for multiple constructors.

Java Instance Initializer Block: More Examples

Example 1: Instance Initializer Block vs Constructor

This example shows that the instance initializer block is executed before the object constructor. Both the instance initializer block and constructor are invoked when an object is created using the new operator.

Code Snippet

package com.tutorialsarena;

public class Tester {
{
    System.out.println("Inside instance initializer block");
}

Tester(){
    System.out.println("Inside constructor");
}

public static void main(String[] arguments) {
    Tester test = new Tester();
}
}

Output


Inside instance initializer block
Inside constructor

Example 2: Constructor Overrides Instance Initializer Block

This example shows that a value initialized in the instance initializer block is overridden by the object constructor. Both the instance initializer block and constructor are invoked when an object is created using the new operator.

Code Snippet

package com.tutorialsarena;

public class Tester {
int a;
{
    System.out.println("Inside instance initializer block");
    a = 10;
}

Tester(){
    System.out.println("Inside constructor");
    a = 20;
}

public static void main(String[] arguments) {
    Tester test = new Tester();
    System.out.println("Value of a: " + test.a);
}
}

Output


Inside instance initializer block
Inside constructor
Value of a: 20

Example 3: Instance Initializer Block and Super Constructor

This example shows that a super constructor is invoked before the child instance initializer block. We have created a SuperTester class, and Tester class extends this class. In the main() method, we print the value of the instance variable. The output verifies the order of blocks invoked: first the super constructor, then the child instance initializer, and finally the child class constructor.

Code Snippet

package com.tutorialsarena;

class SuperTester{
SuperTester(){
    System.out.println("Inside super constructor");
}
}

public class Tester extends SuperTester {
int a;
{
    System.out.println("Inside instance initializer block");
    a = 10;
}

Tester(){
    System.out.println("Inside constructor");
}

public static void main(String[] arguments) {
    Tester test = new Tester();
    System.out.println("Value of a: " + test.a);
}
}

Output


Inside super constructor
Inside instance initializer block
Inside constructor
Value of a: 10