I've created a Java record, and would like to have a constructor that takes in a reduced number of arguments compared to the default constructor, and calculate and initialise all members based on the given arguments.
However, I've found this difficult to achieve given the first line of the custom constructor must call the default constructor. My current approach is to call my calculation functions as needed, but this results in unnecessary processing.
Surely there must be a better way to achieve this?
public record MyRecord(double a, double b, double c) {
public MyRecord(double a) {
this(a, calculateB(a), calculateC(a, calculateB(a)));
}
private static double calculateB(double a) {
// Some calculations
return b;
}
private static double calculateC(double a, double b) {
// Some calculations
return c;
}
}