In Java, how do field initializers and instance initializer blocks interact when assigning final fields, and in what order do they run relative to the constructor?
answer
- Order: prologue → super → initializers+blocks → epilogue
- Initializers/blocks run right after super returns
- Assign a final in exactly one of the three places
- this(...) skips initializers for that constructor
- Illegal forward reference: later field by simple name
basics
~20 sField initializers and instance initializer blocks run in source order, right after the superclass constructor returns and before the statements following super(...). Since JDK 25, statements before super(...) run first. A final field is assigned only once across all of them.
solid answer
~50 sFor each new object, a constructor that reaches `super(...)` runs in this order: since JDK 25, any statements before the call (the **prologue**); then the superclass constructor; then instance field initializers and instance blocks in textual order; then the statements after the call (the **epilogue**). JDK 25 and 27 agree; before 25 there was no prologue. A final instance field must be assigned exactly once across all of this, so if a field initializer or block assigns it, the constructor must not. A constructor delegating with `this(...)` does not run the initializers; they run once per object. Text order also drives the forward-reference rule: an initializer cannot use a later field by simple name, though `this.b` compiles. Until it is assigned, a final can be seen at its default value through a method called during construction, such as an override called by the superclass constructor.
go deeper
Knows initializers and the constructor both run during object creation.
Can state that instance initializers and blocks run in source order, after the superclass constructor and before the statements that follow super(...).
Explains the full prologue → super → initializers/blocks → epilogue order, how a final must be assigned once across all of them, and forward-reference rules.
Reasons about this(...) vs super(...) chains, when initializers fire per object, and how initialization order interacts with inheritance and overridable-method-in-constructor hazards.
## The pieces involved Three mechanisms can set instance state during construction: 1. **Field initializers**: the `= value` part of a field declaration: `private final int a = compute();`. 2. **Instance initializer blocks**: bare `{ ... }` blocks in the class body (not `static {}`): `{ a = compute(); }`. 3. **Constructor bodies**: the statements inside a constructor. Since JDK 25 they divide around an explicit `super(...)` or `this(...)` call into a **prologue** (before the call) and an **epilogue** (after it). A constructor with no explicit call has only an epilogue. ## The construction order (for one object) When `new T(...)` runs, Java does the following **in this order** (JLS 12.5): 1. Allocate the object; all instance fields, final ones included, get default values (0/false/null). 2. The **prologue** runs, if there is one. It cannot use `this`, except for simple assignments to fields of this class that have no initializer. 3. The **superclass constructor** runs (explicit `super(...)`, or an implicit `super()` if you wrote none), fully constructing the superclass portion. 4. **Instance field initializers and instance initializer blocks run, in the exact order they appear in the source text**, interleaved by position. 5. The **epilogue** runs: the statements after the call. Key consequence: by the time the epilogue runs, all field initializers and instance blocks have already run, and the prologue runs before all of them. JDK 25 and JDK 27 behave the same here. Before JDK 25 (JEP 513, after previews in JDK 22 to 24) there was no prologue: `super(...)` or `this(...)` had to come first. (Static initializers and static field initializers are a *separate* sequence. They run once, when the class is **initialized**: immediately before the first instance creation, static method call, static field assignment, or use of a static field that is not a constant variable. Loading is an earlier, separate step: `Class.forName(name, false, loader)` loads a class without running its static blocks.) ## How final fits in A `final` instance field must be **definitely assigned exactly once** considering this whole sequence as one flow. So: - You may assign it in a field initializer **or** an instance block **or** the constructor (prologue or epilogue), but on any path **only one** of these may assign it. - If a field initializer or instance block already assigns the final, the constructor must **not** assign it again, not even before `super(...)`. - Different constructors can rely on a field initializer or block having set the final, since those run for every constructor that does not delegate with `this(...)`. - An initializer or block can read a final assigned in a prologue only when every constructor has an explicit constructor call and the final is assigned before every `super(...)` call. Otherwise the read fails to compile. ## Reading a final before it is set The compiler checks names, not calls. A method called during construction reads whatever the field holds at that moment, and a superclass constructor runs before the subclass's initializers: ```java class Base { Base() { report(); } void report() { } } class Child extends Base { final int blank; final int computed = Integer.parseInt("9"); final int constant = 5; Child() { super(); blank = 7; } @Override void report() { System.out.println(blank + " " + computed + " " + constant); } } ``` Called from `Base()`, this prints `0 0 5`. Only `constant` shows its value, because it is a constant variable whose value the compiler substitutes. Moving `blank = 7;` above `super();` makes it print `7 0 5`: the JLS's own example says a field initialized this way is "impossible to observe" with its default value. ## Why order matters: forward references An initializer cannot use a field declared *later* in the class by its **simple name**, unless it is the left side of an assignment (an 'illegal forward reference', JLS 8.3.3): ```java class C { final int a = b + 1; // does not compile: illegal forward reference final int b = 2; } ``` Reordering so `b` is declared first fixes it. The rule is about source text, not a run-time check, and it does not catch everything: `this.b` compiles and, when `b` is not a constant variable, reads its default value. ## Worked example with a block ```java class Box { private final int w; private final int h; { h = 10; } // instance block sets h Box(int w) { this.w = w; // constructor sets w // must NOT set h again: the block already did } } ``` Here `h` is set by the instance block (which runs before the epilogue) and `w` by the constructor; each final is assigned exactly once. ## A subtle constructor-delegation note If a constructor delegates with `this(...)` instead of `super(...)`, the **field initializers and instance blocks do NOT run for the delegating constructor**: they run only for the constructor that ultimately reaches `super(...)`. They run once per object, not once per constructor in a `this(...)` chain. ## Summary Instance field initializers and instance blocks run in source order, after the superclass constructor and before the epilogue; since JDK 25 a prologue can run before all of them. A `final` field is assigned exactly once across that combined flow; the compiler's definite-assignment analysis spans all of it, and source order also governs legal forward references.
- If a constructor uses this(...) to delegate, do the instance field initializers run for it?They run once per object, triggered by the constructor that invokes super(...). A constructor that delegates via this(...) does not itself trigger the initializers/blocks again; they run as part of the delegated-to chain reaching super().
- Why can't a field initializer read a field declared below it?JLS 8.3.3 rejects a simple-name use to the left of the field's declaration inside an initializer, an illegal forward reference. It is a source-text rule: assigning the field there is allowed, and this.b compiles and can read the default value.
- Can a final field be assigned before super(...)?Yes, since JDK 25, for an instance field declared in the same class without an initializer, when nothing else assigns it. The assignment runs before the superclass constructor, so an overridden method called from there sees the value. Code before the call may not read the field.
- Can code ever see a final field at its default value?Yes. All fields start at their defaults, and the compiler checks names, not calls. An override called by the superclass constructor, or a helper called before the assignment, reads 0 or null. A constant variable is the exception, because its value is substituted at compile time.
- Assuming instance blocks run after the statements that follow super(...)
- Confusing static initialization (once, at class initialization) with instance initialization (per object)
- Setting a final in both an initializer and the constructor
- Thinking field initializers run on every constructor in a this(...) chain
- Saying super(...) must always be a constructor's first statement
- Believing a final field can never be observed at its default value
- flexible constructor bodies
- Java 25pre-release 22
- assigning a field before super(...)
- Java 25pre-release 23
Assumes JDK 27; JDK 25 LTS behaves the same. The prologue exists since JDK 25, where flexible constructor bodies became final; before that super(...) or this(...) had to come first.
context
- Chapter 12. Executiondocs.oracle.com
- Chapter 8. Classesdocs.oracle.com
- JEP 513: Flexible Constructor Bodiesopenjdk.org