87. Defining multiple constructors in a record
As you know, when we declare a Java record, the compiler uses the given components to create a default constructor known as the canonical constructor. We can also provide an explicit canonical/compact constructor as you saw in Problem x.But, we can go even further and declare more constructors with a different list of arguments. For example, we can have a constructor with no arguments for returning a default instance:
public record MelonRecord(String type, float weight) {
private static final String DEFAULT_MELON_TYPE = “Crenshaw”;
private static final float DEFAULT_MELON_WEIGHT = 1000;
MelonRecord() {
this(DEFAULT_MELON_TYPE, DEFAULT_MELON_WEIGHT);
}
}
Or, we can write a constructor that gets as an argument only the melon’s type or the melon’s weight:
public record MelonRecord(String type, float weight) {
private static final String DEFAULT_MELON_TYPE = “Crenshaw”;
private static final float DEFAULT_MELON_WEIGHT = 1000;
MelonRecord(String type) {
this(type, DEFAULT_MELON_WEIGHT);
}
MelonRecord(float weight) {
this(DEFAULT_MELON_TYPE, weight);
}
}
Moreover, we can add arguments that don’t fit any component (here, country):
public record MelonRecord(String type, float weight) {
private static Set<String> countries = new HashSet<>();
MelonRecord(String type, int weight, String country) {
this(type, weight);
MelonRecord.countries.add(country);
}
}
What do all these constructors have in common? They all call the canonical constructor via this keyword. Remember that the only way to instantiate a Java record is via its canonical constructor which can be called directly or, as you saw in the previous examples, indirectly. So, keep in mind that all explicit constructors that you add to a Java record must call first the canonical constructor.
88. Implementing interfaces in records
Java records cannot extend another class but they can implement any interface exactly as a typical class. Let’s consider the following interface:
public interface PestInspector {
public default boolean detectPest() {
return Math.random() > 0.5d;
}
public void exterminatePest();
}
The following snippet of code is a straightforward usage of this interface:
public record MelonRecord(String type, float weight)
implements PestInspector {
@Override
public void exterminatePest() {
if (detectPest()) {
System.out.println(“All pests have been exterminated”);
} else {
System.out.println(
“This melon is clean, no pests have been found”);
}
}
}
Notice that the code overrides the abstract method exterminatePest() and calls the default method detectPest().