Instance Initializer block in java

Instance initializer block:

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

Example:

AnonymousBlockExample1.java

/**
 * This program is used to show the use of AnonymousBlock.
 * @author W3spoint
 */
class Display {
      int a, b;
 
      //Anonymous or instance initializer Block
      {
          System.out.println("AnonumousBlock called.");
          a = 10;
      }
 
     //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(20);
              obj2.display();
       }
}

Output:

AnonumousBlock called.
default constructor called.
a = 10
b = 0
AnonumousBlock called.
one parameter constructor called.
a = 10
b = 20

Download this example.

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

Example:

AnonymousBlockExample2.java

/**
 * This program is used to show that if two AnonymousBlocks
 * are used then they will execute in the same order in
 * which they are appear.
 * @author W3spoint
 */
class Display {
      int a, b, c;
 
      //First Anonymous or instance initializer Block
      {
         System.out.println("First AnonumousBlock called.");
         a = 10;
      }
 
      //Second Anonymous or instance initializer Block
      {
          System.out.println("Second AnonumousBlock called.");
          b = 20;
      }
 
      //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(30);
              obj2.display();
       }
}

Output:

First AnonumousBlock called.
Second AnonumousBlock called.
default constructor called.
a = 10
b = 20
c = 0
First AnonumousBlock called.
Second AnonumousBlock called.
one parameter constructor called.
a = 10
b = 20
c = 30

Download this example.

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

Example:

AnonymousBlockExample3.java

/**
 * This program is used to show that if static and non-static
 * AnonymousBlocks are used then static AnonymousBlocks is
 * executed only once.
 * @author W3spoint
 */
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 = 20;
      }
 
      //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(30);
              obj2.display();
      }
}

Output:

Static AnonumousBlock called.
Non-Static AnonumousBlock called.
default constructor called.
a = 20
b = 0
Non-Static AnonumousBlock called.
one parameter constructor called.
a = 20
b = 30

Download this example.

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

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

Example:

AnonymousBlockExample4.java

/**
 * This program is used to show that in which order static
 * AnonumousBlocks, non-static AnonumousBlocks, super and
 * default constructors are called.
 * @author W3spoint
 */
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.

Download this example.
 
Next Topic: super in java with example.
Previous Topic: this in java with example.

 

Content Protection by DMCA.com
Please Share