Flexible constructor bodies
Validate and compute values before calling super() or this().
Porównanie kodu
✕ Java 8
class Square extends Shape {
Square(double side) {
super(side, side);
// can't validate BEFORE super!
if (side <= 0)
throw new IAE("bad");
}
}
✓ Java 25+
class Square extends Shape {
Square(double side) {
if (side <= 0)
throw new IAE("bad");
super(side, side);
}
}
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
Fail fast
Validate arguments before the superclass constructor runs.
Compute first
Derive values and prepare data before calling super().
No workarounds
No more static helper methods or factory patterns to work around the restriction.
Stare podejście
Validate After super()
Nowoczesne podejście
Code Before super()
Od JDK
25
Poziom trudności
Średniozaawansowany
Wsparcie JDK
Flexible constructor bodies
Dostępne
Finalized in JDK 25 LTS (JEP 513, Sept 2025).
Jak to działa
Java 25 lifts the restriction that super() must be the first statement. You can now validate arguments, compute derived values, and set up state before delegating to the parent constructor.
Powiązana dokumentacja