Quick revision
The short answer
With Java 25 we can validate first and then call super: PositiveRange(long lo, long hi) { if (lo < 0) throw new IllegalArgumentException(); super(lo, hi); }. The statements before super(...) or this(...) form the prologue, where we may validate arguments, compute values to pass up and assign fields of the class being constructed that have no initializer, but may not read fields, call instance methods or otherwise use this. The feature was previewed in JDK 22 to 24 and is final in Java 25 through JEP 513.
What to remember
In this answer, we rewrote PositiveRange so it validates lo before calling super(lo, hi), which Java 25's flexible constructor bodies allow. The prologue before super(...) or this(...) may use parameters and locals, call static methods, throw, compute arguments and assign the class's own fields that have no initializer, but may not read fields, call instance methods or use this in any other way. Assigning fields early also fixes the old problem of overridable methods seeing default values, and the feature went from preview in JDK 22 to 24 to final in Java 25 through JEP 513.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With Java 25 we can validate first and then call super: PositiveRange(long lo, long hi) { if (lo < 0) throw new IllegalArgumentException(); super(lo, hi); }. The statements before super(...) or this(...) form the prologue, where we may validate arguments, compute values to pass up and assign fields of the class being constructed that have no initializer, but may not read fields, call instance methods or otherwise use this. The feature was previewed in JDK 22 to 24 and is final in Java 25 through JEP 513.
Why this question is asked
For almost thirty years, Java required super(...) or this(...) to be the very first statement in a constructor. That rule made simple things awkward, like rejecting a bad argument before the superclass does expensive work with it. Java 25 finally relaxed it in a careful way. Interviewers ask this question to check that we can use the new syntax and, more importantly, that we know exactly what is still forbidden and why. In this answer, we will look at the problem with the original code, rewrite it, go through what the prologue may and may not contain, see how it fixes the classic overridable-method problem, and recap the feature's history.
1What Is Wrong With the Original Code
The original constructor calls super(lo, hi) first and validates afterwards, because before Java 25 nothing could come before super.
Notice the order. The superclass constructor does all its work on arguments that the subclass will reject a moment later. If that work is expensive, or has side effects such as registering the object, we pay for it and may leave traces of an object that never existed. The usual workaround was a static helper that checks and returns its argument, written as super(checkLo(lo), hi), which works but hides the validation inside an argument list.
2The Rewrite in Java 25
With flexible constructor bodies, the check goes where it belongs.
The constructor reads top to bottom in the order things happen: check the input, then build the object. If the check fails, no superclass code runs at all.
3What the Prologue May and May Not Do
The statements before the explicit constructor call are called the prologue, and the statements after it the epilogue. During the prologue the object exists but is not initialized, its superclass part has not been constructed yet, so the rules keep us from touching it. We may use the constructor's parameters and local variables, call static methods, create other objects, throw exceptions, and compute values to pass to super(...) or this(...). We may also assign fields declared in this class, as long as the field has no initializer.
We may not use this in any other way. That means no reading of fields, even one we just assigned, no calls to instance methods, no passing this to another method, no super.field or super.method(), and no creating an instance of an inner class of this class, because that would capture this. The compiler enforces all of these.
| In the prologue | Allowed? | Why |
|---|---|---|
| Use parameters and locals | Yes | they do not involve the object |
| Call static methods, throw | Yes | no access to this |
Compute arguments for super(...) | Yes | the main purpose of the feature |
| Assign own field with no initializer | Yes | a write; nothing reads an unset value |
| Read a field | No | the object is not initialized yet |
| Call an instance method, pass this | No | code could see a half-built object |
super.x, new Inner() | No | both use or capture this |
4Assigning Fields First Fixes a Classic Bug
The field-assignment rule solves an old problem. If a superclass constructor calls an overridable method, the subclass override runs before the subclass fields are set, and sees null or 0. Assigning the field in the prologue changes that.
This is why the rule allows writes but not reads: a write cannot observe an unfinished object, while a read could see a default value. Calling overridable methods from constructors is still bad practice, but when we cannot change the superclass, this gives us a clean fix.
5The History of the Feature
The change went through three previews before it became final. It started as statements before super(...) in JDK 22 (JEP 447), was extended in JDK 23 (JEP 482) to allow assigning fields in the prologue and renamed flexible constructor bodies, previewed a third time in JDK 24 (JEP 492), and was finalized in Java 25 (JEP 513) with no further changes. So code that uses it needs to be compiled with --release 25 or later, and no preview flag is needed from Java 25 onwards.
6Follow-up questions
Does the JVM need to change for this?
Very little. The JVM's verifier has long allowed some code before the superclass constructor call, as long as it does not use the uninitialized object, which javac itself relied on for inner classes. The feature is mainly a change to the language rules and the compiler's checks.
Can a record's canonical constructor use this?
A record's canonical and compact constructors cannot call super(...) explicitly, and a non-canonical record constructor must start by delegating with this(...). With flexible constructor bodies, a non-canonical record constructor may now validate or compute arguments before that this(...) call.
Why not just allow any code before super(...)?
Because then code could read fields or call methods on an object whose superclass part does not exist yet, which is exactly the class of bugs the original rule prevented. The prologue rules keep the old safety guarantee, nothing observes an uninitialized object, while removing the restriction on code that cannot observe it.
7Conclusion
In this answer, we rewrote PositiveRange so it validates lo before calling super(lo, hi), which Java 25's flexible constructor bodies allow. The prologue before super(...) or this(...) may use parameters and locals, call static methods, throw, compute arguments and assign the class's own fields that have no initializer, but may not read fields, call instance methods or use this in any other way. Assigning fields early also fixes the old problem of overridable methods seeing default values, and the feature went from preview in JDK 22 to 24 to final in Java 25 through JEP 513.
Open with
In Java 25 I would write the constructor as: if lo is less than zero, throw IllegalArgumentException, and then call super with lo and hi. That way an invalid range is rejected before Range's constructor does any work, whereas the original code builds the superclass part first and only then rejects it, which wastes work and can leave side effects.
Then, if they want more
The code before the super call is the prologue. In it I can use the parameters and local variables, call static methods, create objects that do not involve this, throw exceptions and compute the arguments to pass up, which replaces the old trick of a static helper inside the super call.
I can also assign fields declared in my own class, as long as they have no initializer. What I cannot do is use this in any other way: no reading fields, even ones I just assigned, no instance method calls, no passing this out, no super dot anything, and no creating an inner class instance.
The reason is that the superclass part does not exist yet, so anything that could observe the object is forbidden, while writes are allowed. That field assignment also fixes the classic bug where a superclass constructor calls an override that sees null. The feature was previewed in JDK 22, 23 and 24 and finalized in Java 25 by JEP 513.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 22, Java 23, Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Java 25 finalised flexible constructor bodies (JEP 513). Rewrite this using the feature...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/9b508c3601/