How does static initializer execution relate to instance initializers and constructors across a class hierarchy when you create an object?
answer
- Static once (class init); instance every new (object init)
- Statics: parent then child, top-down, before any object
- Per object: parent instance+ctor, then child instance+ctor
- Instance block runs after super(), before ctor body
- Two news → static lines once, instance lines twice
basics
~20 sStatic blocks run first and only once (when the class is first loaded). Then for each new object: the parent's instance setup and constructor run, then the child's. Static = once per class; instance initializers/constructors = once per object.
solid answer
~50 sStatic initialization and instance initialization are two different phases. Static blocks and static field initializers run **once**, when the class is first initialized — superclass static before subclass static — before any object exists. Then, every time you do `new`, the **instance** sequence runs per object: for each class in the hierarchy from the top down, the JVM runs that class's instance field initializers and instance initializer blocks (`{ }`) in textual order, then the constructor body. Because of constructor chaining (`super(...)` runs first), the effective order for `new Child()` is: (one-time) Parent static → Child static; then (per object) Parent instance init + Parent constructor body → Child instance init + Child constructor body. A classic interview output question relies on exactly this: all statics print once at first use, then the instance lines print parent-first for every new object.
go deeper
Knows static blocks run once and constructors run per object, and that parent comes before child.
Can lay out the full order: statics (parent→child, once) then per-object parent instance init + ctor then child instance init + ctor.
Explains why instance initializers splice in after super() and before the ctor body, and predicts multi-object output correctly.
Uses this precise model to reason about subtle initialization bugs (e.g. virtual calls during construction seeing default field values) and to advise against complex initialization ordering dependencies.
## Two separate phases: class-level vs. object-level Java has **static initialization** (class-level, runs once) and **instance initialization** (object-level, runs every `new`). Static initializer blocks belong to the first; instance initializer blocks and constructors belong to the second. Understanding how they interleave is a staple 'predict the output' interview question. ## The four kinds of initializer - **Static field initializer**: `static int a = 1;` - **Static block**: `static { ... }` - **Instance field initializer**: `int b = 2;` - **Instance block**: `{ ... }` - (plus the **constructor** body) ## Full ordering for `new Child()` where `Child extends Parent` **Phase 1 — class initialization (happens ONCE, at first active use of `Child`):** 1. Parent's static field initializers + static blocks, in textual order. 2. Child's static field initializers + static blocks, in textual order. (Superclass is always initialized before the subclass.) **Phase 2 — instance creation (happens for EVERY `new`):** 3. Parent's instance field initializers + instance blocks, in textual order. 4. Parent's constructor body. 5. Child's instance field initializers + instance blocks, in textual order. 6. Child's constructor body. Why does parent instance setup precede child? Because the child constructor implicitly (or explicitly) calls `super(...)` as its **first** action. Within a single class, the instance field initializers and instance blocks run **after** the implicit `super()` returns but **before** the rest of the constructor body — they're effectively spliced in right after the `super()` call. ## Worked example ```java class Parent { static { System.out.println("1 Parent static"); } { System.out.println("3 Parent instance init"); } Parent() { System.out.println("4 Parent ctor"); } } class Child extends Parent { static { System.out.println("2 Child static"); } { System.out.println("5 Child instance init"); } Child() { System.out.println("6 Child ctor"); } } new Child(); new Child(); ``` Output: ``` 1 Parent static 2 Child static 3 Parent instance init 4 Parent ctor 5 Child instance init 6 Child ctor 3 Parent instance init 4 Parent ctor 5 Child instance init 6 Child ctor ``` Notice the static lines (`1`, `2`) print **only once**, on the first `new Child()`; the instance lines (`3`–`6`) repeat for the second object, always **parent-first**. ## Common gotchas - **Statics print once, instances every time.** If you create two objects, the static lines do not repeat. - **Instance blocks run before the constructor *body*, not before `super()`.** Within a class, the order is: super() → that class's instance initializers/blocks → that class's constructor body. - **Textual order applies within each group** — multiple instance blocks and field initializers interleave in source order, just like statics do. - **A static block can run without any object** — merely calling a static method triggers Phase 1 but never Phase 2. ## Mental shortcut For `new Child()`: think 'statics top-down, once' then 'per object: parent instance + ctor, then child instance + ctor.' If asked about two objects, repeat only the instance part.
- If you create three objects of Child, how many times do the static blocks print?Once total — static initialization runs a single time at the class's first active use, regardless of how many instances you create.
- Where do a class's instance initializer blocks run relative to its constructor body?After the implicit/explicit super(...) call returns, and before the rest of that class's constructor body — they're effectively inserted at the top of the constructor (after super()).
saying these in an interview costs you the question
- Saying static blocks run for every object (they run once at class init)
- Putting the child's constructor before the parent's (super runs first)
- Claiming instance initializer blocks run before super() (they run after)
- Mixing up static and instance initializer ordering as if they were one phase