Skip to content

Java 25: Flexible constructor bodies (JEP 513) - #99

Merged
mperor merged 2 commits into
masterfrom
DAILY/86-flexible-constructor-bodies
Oct 22, 2026
Merged

mperor merged 2 commits into
masterfrom
DAILY/86-flexible-constructor-bodies

Conversation

@mperor

@mperor mperor commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

What you'll learn

  • Statements can now run before super(...)/this(...) — the constructor "prologue"
  • Argument validation/transformation no longer needs a private static helper method called inside super(...)
  • You may assign (but not read) your own fields in the prologue, so an overridden method invoked from the superclass constructor already sees them initialized
  • this still cannot be referenced in the prologue: no reading fields, no calling instance methods, no leaking this

Why it matters

Avoids the classic workaround of smuggling pre-super() logic through static helper methods or constructor-chaining tricks, making validation and field setup read top-to-bottom like normal code.

Read more

JEP 513: Flexible Constructor Bodies

Quiz

In Circle, the field radius is assigned before super() is called, and Shape's constructor calls the overridable describe() method. What does circle.description hold after new Circle(2.5)?

Answer "circle r=2.5" — even though `describe()` runs while `Shape`'s constructor is still executing (before `Circle`'s own `super()` call returns), `radius` was already assigned in the prologue, so the override sees the real value instead of the default `0.0`.

Closes #86

@mperor mperor added the daily Queued for the daily learning PR label Oct 22, 2026
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Test Results

262 tests  +2   259 ✅ +2   5s ⏱️ ±0s
 78 suites ±0     3 💤 ±0 
 78 files   ±0     0 ❌ ±0 

Results for commit 613bca9. ± Comparison against base commit cbd4af3.

♻️ This comment has been updated with latest results.

@mperor
mperor merged commit b209ba5 into master Oct 22, 2026
2 checks passed
@mperor
mperor deleted the DAILY/86-flexible-constructor-bodies branch October 22, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

daily Queued for the daily learning PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java 25: Flexible constructor bodies (JEP 513)

2 participants