
class Shape {
final int area;
public Shape(int area) {
if (area <= 0) throw new IllegalArgumentException("Area must be positive.");
this.area = area;
}
}
Ahora di que quieres tener un Rectangle clase. En Java anterior a JDK 25, tendrías que extraer de alguna manera el cálculo para usarlo en el super() llamada, generalmente usando un método estático:
// 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 ...
}
}
Este código es bastante torpe. Pero en Java 25, es más fácil seguir tu intención y ejecutar el cálculo del área en el 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;
}
}
Importaciones de módulos al por mayor
Otra característica finalizada en JDK 25, JEP 511: Declaraciones de importación de módulosle permite importar un módulo completo en lugar de tener que importar cada paquete uno por uno.




