instance initializer block in Java



Instance initializer block works are used to initialize the properties of an object. It is invoked before the constructor is invoked. It is invoked every time an object is created. See the example below −

Example

Live Demo

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();
      Tester test1 = new Tester();
   }
}

Output

Inside instance initializer block
Inside constructor
Inside instance initializer block
Inside constructor

Advertisements