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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now