oop

20. Initialization Order: The Grand Sequence

Analyze the code, write your predicted output, and then reveal the solution.

Scenario

When you create a child object, what runs first? Static blocks? Constructors? Parent or Child? This is the ultimate test of your understanding of object lifecycle.

Code Block
class Base { static { System.out.println("Base: Static Block"); } { System.out.println("Base: Instance Block"); } Base() { System.out.println("Base: Constructor"); } } class Derived extends Base { static { System.out.println("Derived: Static Block"); } { System.out.println("Derived: Instance Block"); } Derived() { super(); System.out.println("Derived: Constructor"); } } public class Main { public static void main(String[] args) { System.out.println("--- First Object ---"); new Derived(); System.out.println("--- Second Object ---"); new Derived(); } }
Take Your Shot
Predicted Output

Type exactly what you think will be printed in the console.

20. Initialization Order: The Grand Sequence | DevExCode