Java Instance Initializer block

Java Instance Initializer block is a mechanism provided by the Java compiler to define a group of statements common to all constructors in a single place. At the compilation time, the compiler moves these statements at the beginning of all constructors after super. It can also be used to initialize the instance variable.

Java Instance Initializer block Example

class Display {
      int a, b;

      //Anonymous or instance initializer Block
      {
          System.out.println("AnonumousBlock called.");
          a = 100;
      }

     //default constructor
     Display(){
        System.out.println("default constructor called.");
     }

     //one argument constructor
     Display(int num){
        System.out.println("one parameter constructor called.");
        b = num;
     }

     //method to display values
     public void display(){
            System.out.println("a = " + a);
            System.out.println("b = " + b);
     }
}

public class AnonymousBlockExample1 {
       public static void main(String args[]){
              Display obj1 = new Display();
              obj1.display();

              Display obj2 = new Display(200);
              obj2.display();
       }
}

Output

AnonumousBlock called.
default constructor called.
a = 100
b = 0
AnonumousBlock called.
one parameter constructor called.
a = 100
b = 200

If two Anonymous Blocks are used then they will execute in the same order in which they appear.

class Display {
      int a, b, c;

      //First Anonymous or instance initializer Block
      {
         System.out.println("First AnonumousBlock called.");
         a = 100;
      }

      //Second Anonymous or instance initializer Block
      {
          System.out.println("Second AnonumousBlock called.");
          b = 200;
      }

      //default constructor
      Display(){
          System.out.println("default constructor called.");
      }

      //one argument constructor
      Display(int num){
              System.out.println("one parameter constructor called.");
              c = num;
     }

     //method to display values
     public void display(){
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            System.out.println("c = " + c);
     }
}

public class AnonymousBlockExample2 {
       public static void main(String args[]){
              Display obj1 = new Display();
              obj1.display();

              Display obj2 = new Display(300);
              obj2.display();
       }
}

Output

First AnonumousBlock called.
Second AnonumousBlock called.
default constructor called.
a = 100
b = 200
c = 0
First AnonumousBlock called.
Second AnonumousBlock called.
one parameter constructor called.
a = 100
b = 200
c = 300

If static and non-static Anonymous Blocks are used then static Anonymous Block is executed only once.

class Display {
      int a, b;

      //static Anonymous or instance initializer Block
      static {
              System.out.println("Static AnonumousBlock called.");
      }

      //non-static Anonymous or instance initializer Block
      {
          System.out.println("Non-Static AnonumousBlock called.");
          a = 200;
      }

      //default constructor
      Display(){
          System.out.println("default constructor called.");
      }

      //one argument constructor
      Display(int num){
         System.out.println("one parameter constructor called.");
         b = num;
      }

      //method to display values
      public void display(){
             System.out.println("a = " + a);
             System.out.println("b = " + b);
     }
}

public class AnonymousBlockExample3 {
       public static void main(String args[]){
              Display obj1 = new Display();
              obj1.display();

              Display obj2 = new Display(300);
              obj2.display();
      }
}

Output

Static AnonumousBlock called.
Non-Static AnonumousBlock called.
default constructor called.
a = 200
b = 0
Non-Static AnonumousBlock called.
one parameter constructor called.
a = 200
b = 300

In which order static initializer block, instance initializer block, super, and constructor are called?

static initialize block –> super- instance initialize block –> constructor.

class Show{
      Show(){
             System.out.println("Super class constructor.");
      }
}

class Display extends Show{
      //static Anonymous or instance initializer Block
      static {
              System.out.println("Static AnonumousBlock called.");
      }

      //non-static Anonymous or instance initializer Block
      {
        System.out.println("Non-Static AnonumousBlock called.");
      }

      //default constructor
      Display(){
           super();
           System.out.println("default constructor called.");
      }
}

public class AnonymousBlockExample4 {
       public static void main(String args[]){
               Display obj = new Display();
       }
}

Output

Static AnonumousBlock called.
Super class constructor.
Non-Static AnonumousBlock called.
default constructor called.