statements before super(...)statements before super(...)
HappyCoders Glasses

Statements before super(...): Java Code in the Constructor

Sven Woltmann
Sven Woltmann
Last update: January 11, 2024

In this article, you will learn:

  • how to execute code in constructors before calling super(...) or this(...) from Java 22 onwards,
  • what restrictions exist,
  • what the prologue and epilogue of a constructor are,
  • and whether the new features also apply to records and enums.

Let’s take a step back: Why would you want to execute code before super(...) or this(...)?

Code in Constructors – Status Quo Before Java 22

The following two examples show workarounds that were previously required to validate or compute parameters before calling super() or this().

Use Case 1: Parameter Validation

An everyday use case is the validation of parameters of a child class. In the following example, the constructor of Rectangle first calls the constructor of the parent class, Shape, and then validates and sets the width and height:

public class Shape {
  private final Color color;

  public Shape(Color color) {
    this.color = color;
  }
}

public class Rectangle extends Shape {
  private final double width;
  private final double height;

  public Rectangle(Color color, double width, double height) {
    super(color);
    if (width < 0 || height < 0) throw new IllegalArgumentException();
    this.width = width;
    this.height = height;
  }
}Code language: Java (java)

However, validating the parameters before the super constructor is called would be much more efficient. Yet, this is currently only possible with the following extremely unappealing workaround:

public Rectangle(Color color, int width, int height) {
  super(validateParams(color, width, height));
  this.width = width;
  this.height = height;
}

private static Color validateParams(Color color, int width, int height) {
  if (width < 0 || height < 0) throw new IllegalArgumentException();
  return color;
}Code language: Java (java)

Use Case 2: Calculation of an Argument That Is Passed To Several Parameters

Another use case is the calculation of values that are to be passed on to more than one superclass constructor parameter. In the following example, we want to create a square with a given area (we will ignore the fact that a static factory method with a meaningful name would be more suitable than a constructor):

public class Square extends Rectangle {
  public Square(Color color, int area) {
    super(color, Math.sqrt(area), Math.sqrt(area));
  }
}Code language: Java (java)

To avoid calculating the square root of the area twice, we would have to introduce an auxiliary constructor:

public class Square extends Rectangle {
  public Square(Color color, int area) {
    this(color, Math.sqrt(area));
  }

  private Square(Color color, double sideLength) {
    super(color, sideLength, sideLength);
  }
}Code language: Java (java)

However, this is only possible here because area is of the type int. If area were like sideLength of type double, this would not work, as we would then have two constructors with identical signatures.

And if we wanted to make sure that area is not negative beforehand, we would have to introduce a third method, as we are not allowed to execute any other code before this(...):

public class Square extends Rectangle {
  public Square(Color color, int area) {
    this(color, Math.sqrt(validateArea(area)));
  }

  private static double validateArea(int area) {
    if (area < 0) throw new IllegalArgumentException();
    return area;
  }

  private Square(Color color, double sideLength) {
    super(color, sideLength, sideLength);
  }
}Code language: Java (java)

It is hard to see what this code does.

Java Code Before super(...) and this(...)

With JDK Enhancement Proposal 447, Java 22 introduces – for the time being as a preview feature – the possibility of executing code before calling super(...) or this(...).

That means that we can now validate the area before calling this(...):

public class Square extends Rectangle {
  public Square(Color color, int area) {
    if (area < 0) throw new IllegalArgumentException();
    this(color, Math.sqrt(area));
  }

  private Square(Color color, double sideLength) {
    super(color, sideLength, sideLength);
  }
}Code language: Java (java)

And we no longer need the auxiliary constructor either. We can now accommodate the parameter validation and the calculation of the side length directly in the constructor:

public Square(Color color, int area) {
  if (area < 0) throw new IllegalArgumentException();
  double sideLength = Math.sqrt(area);
  super(color, sideLength, sideLength);
}Code language: Java (java)

With this constructor, you can see at a glance what the code does.

Constructor Prologue and Epilogue

The block before super(...) or this(...) is called the “prologue”.

Code after calling super(...) or this(...) or code in a constructor without calling super(...) or this(...) is called the “epilogue”.

Restrictions

In the prologue, the code must not read any fields of the class and must not call any non-static methods of the class. It must also not create instances of non-static inner classes, as these would then have a reference to the potentially uninitialized parent object.

The prologue of the constructor of an inner class, on the other hand, can access fields and methods of the outer class.

Records and Enums

Records and enums cannot have a parent class, but their constructors can call alternative constructors with this(...).

Code that complies with the abovementioned restrictions may now also be executed before that.

Conclusion

Calling code before super(...) or this(...) allows parameters to be validated or calculated before the super constructor or an alternative constructor is called. That enables much more expressive code than the workarounds we had to construct in the past.

Have you also had to implement complicated workarounds, and what do you think of the new feature? Let me know via the comment function!

You don’t want to miss any HappyCoders.eu article and always be informed about new Java features? Then click here to sign up for the free HappyCoders newsletter.