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.
3 points to say:
- Statics run once when the class is initialized, parent before child.
- The parent constructor runs before the child's fields are assigned: an overridden method called from it sees
null,0, even forfinalfields. - Never call overridable methods from a constructor, and don't let
thisescape.
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.