instance initializer block in Java

JavaJava Programming Java 8

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
raja
Published on 02-Feb-2018 11:09:26
Advertisements