SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 7 / 42

Middle

What is Java's object initialization order, and why not call overridable methods from a constructor?

Short answer

On the first new Child(): parent statics, then child statics (once). Then for every object: parent instance → parent constructor → child instance → child constructor.

each objectParent staticChild staticParent instance:fields, blocksParent constructorChild instance:fields, blocksChild constructor

3 points to say:

  1. Statics run once when the class is initialized, parent before child.
  2. The parent constructor runs before the child's fields are assigned: an overridden method called from it sees null, 0, even for final fields.
  3. Never call overridable methods from a constructor, and don't let this escape.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.