Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Instance Initializer Block



Java Instance Initializer Block

An instance initializer block is a block of code that is declared inside a class to initialize the instance data members. Instance Initializer block 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 instance initializer block in Java:

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

Characteristics of Instance Initializer Block

  • Instance initializer block is called once an object is created.

  • Instance initializer block is called before any constructor of an object is invoked.

  • In case of child class, Instance initializer block will be called after super class constructor call.

  • Instance initializer block can be used to execute multiple statements.

  • Instance initializer block is generally used to instantiate multiple values fields like arrays.

Use of Instance Initializer Block

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

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

Java Instance Initializer Block: More Examples

Example 1: Demonstrating What Invokes First, Instance Initializer Block or Constructor

In this example, we've shown that instance initializer block is getting executed before the object constructor. Both instance initializer block and constructor are invoked when object is created using new operator.

package com.tutorialspoint;
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

60
150

Example 2: Demonstrating Whether Constructor Overrides Instance Initializer Block

In this example, we've shown that a value initialized in instance initializer block is getting overriden by the object constructor. Both instance initializer block and constructor are invoked when object is created using new operator.

package com.tutorialspoint;
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: " + a);
   }
}

Output

Inside instance initializer block
Inside constructor
Value of a: 20

Example 3: Instance Initializer Block and Super Constructor

In this example, we've shown that a super constructor is invoked before child instance initializer block. We've created a SuperTester class and Tester class extends this class. In main() method, we're printing the value of instance variable. In output, you can verify the order of blocks invoked. First super constructor is invoked. Then child instance initializer is invoked which initializes an instance variable and then constructor of child class is invoked.

package com.tutorialspoint;
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
Advertisements