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.
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.
Output:
Explanation:
In the above program, we created two classes Sample and Main. The Sample class contains an IIB (Instance Initializer Block) and a constructor.