Defining multiple constructors in a record – Record and record pattern

0 Comments 5:38 AM

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().

Leave a Reply

Your email address will not be published. Required fields are marked *

Understanding records serialization 2 – Record and record patternUnderstanding records serialization 2 – Record and record pattern

Serializing/deserialzing gacContainer (typical Java class) The gacContainer object is an instance of MelonContainer which is a plain Java class. MelonContainer gacContainer = new MelonContainer(  LocalDate.now().plusDays(15), “ML9000SQA0”,    new Melon(“Gac”, 5000)); After

Adding more artifacts in a record Certification Exams of Java Getting a list from a stream Java Exams

Covering Vector API structure and terminology – Arrays, collections and data structuresCovering Vector API structure and terminology – Arrays, collections and data structures

102. Covering Vector API structure and terminology The Vector API is mapped by the jdk.incubator.vector module (and a package with the same name). A jdk.incubator.vector.Vector starts from a generic abstract

Certification Exams of Java Getting a list from a stream Java Exams Tackling guarded record patterns Tackling records in Spring Boot