class Shape {
final int area;
public Shape(int area) {
if (area <= 0) throw new IllegalArgumentException("Area must be positive.");
this.area = area;
}
}
Now say you want to have a Rectangle
class. In Java before JDK 25, you’d have to somehow extract the calculation to use it in the super()
call, usually using a static method:
// The old way
class Rectangle extends Shape {
private static int checkAndCalcArea(int w, int h) {
if (w <= 0 || h <= 0) {
throw new IllegalArgumentException("Dimensions must be positive.");
}
return w * h;
}
public Rectangle(int width, int height) {
super(checkAndCalcArea(width, height)); // super() had to be first
// ... constructor logic ...
}
}
This code is quite clunky. But in Java 25, it’s easier to follow your intention, and run the area calculation in the Rectangle
constructor:
class Rectangle extends Shape {
final int width;
final int height;
public Rectangle(int width, int height) {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("Dimensions must be positive.");
}
int area = width * height;
super(area); // Before 25, this was an error
this.width = width;
this.height = height;
}
}
Wholesale module imports
Another feature finalized in JDK 25, JEP 511: Module import declarations, lets you import an entire module instead of having to import each package one by one.