Q:

Java program to call an Instance Initializer Block using the anonymous object

0

Java program to call an Instance Initializer Block using the anonymous object

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to call an Instance Initializer Block using an anonymous object is given below. The given program is compiled and executed successfully.

// Java program to call an Instance Initializer Block 
// using the anonymous object

class Sample {
  {
    System.out.println("Instance initializer block called");
  }

  Sample() {
    System.out.println("Constructor called");
  }

}
public class Main {
  public static void main(String[] args) {
    new Sample();
  }
}

Output:

Instance initializer block called
Constructor called

Explanation:

In the above program, we created two classes Sample and Main. The Sample class contains an IIB (Instance Initializer Block) and a constructor.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to initialize instance data member us... >>
<< Java program to create multiple Instance Initializ...