Java Instance Initializer Block

Java instance initializers are the code blocks containing the instructions to run everytime a new class instance is created. We can use the initializer blocks to write initialization login common to all constructors, which otherwise must be written in each constructor separately.

1. Syntax

An instance initializer block is created with curly braces. The object initialization statements are written inside the braces.

public class DemoClass {

  public DemoClass() {
    //statements
  }

  /* initializer block */
  { 
    //statements
  }
}

2. How does the Instance Initializer Block Works?

During compile time, the Java compiler copies the bytecode of the initializer block immediately after the super() statement in each constructor.

public class Main {

  public Main() {
    System.out.println("Statement in constructor");
  }

  {
    System.out.println("Statement in initializer");
  }
}

Consider the above program where the constructor and initializer blocks have one print statement. After the code is compiled, the bytecode is logically structured as follows:

public class Main {

  public Main() {
    super();
    System.out.println("Statement in initializer");
    System.out.println("Statement in constructor");
  }
}

we can verify this structuring by executing the program, which prints the following output:

Statement in initializer
Statement in constructor

3. Initializer Blocks Run in Sequence

If there are multiple initializer blocks in a class, they are executed in the same sequence they appear in the class definition. In following example, the initializer block 1 executes before block 2, always.

public class Main {

  public Main() {

    System.out.println("Statement in constructor 2");
  }

  {
    System.out.println("Statement in initializer 1");
  }

  {
    System.out.println("Statement in initializer 2");
  }
}

The program outputs when an instance is created using the default constructor:

Statement in initializer 1
Statement in initializer 2
Statement in constructor 2

4. Inheritance

When the parent class and the child class, both have the initializer blocks, the sequence of flow is as follows when an instance of the Child class is created:

  • Child class constructor is invoked.
  • Child class constructor has the first statement as super() (or provided explicit constructor), so the parent class constructor is invoked.
  • Parent‘s initializers are executed in the sequence of their appearance.
  • Parent‘s constructor statements are executed.
  • Child class’s initializers are executed in the sequence of their appearance.
  • Child class constructor statements are executed.

Let us see the above in action:

class ParentClass {

  public ParentClass() {
    System.out.println("In ParentClass Constructor");
  }

  {
    System.out.println("In ParentClass Instance Initializer");
  }
}

class ChildClass extends ParentClass {

  public ChildClass() {
    super();  //If not provided, JVM will insert it
    System.out.println("In ChildClass Constructor");
  }

  {
    System.out.println("In ChildClass Instance Initializer 1");
  }

  {
    System.out.println("In ChildClass Instance Initializer 2");
  }
}

When we create an instance of the ChildClass the program outputs:

In ParentClass Instance Initializer
In ParentClass Constructor

In ChildClass Instance Initializer 1
In ChildClass Instance Initializer 2
In ChildClass Constructor

5. Features of Instance Initializers

The instance initializers have the following features.

  • We can define multiple initializers in a class.
  • All initializers execute in sequence in the order they appear in the class body.
  • Initializers run after the parent class constructor has been invoked and before executing the child class constructor. Please note that Java inserts the default constructor of parent class super(), if we do not explicitly provide the constructor as the first statement in the child class’s constructor.
  • After all the initializers have been executed, the constructor’s statements are executed.
  • We can use call the constructors of this class and the parent class inside initializers.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode