Define default constructor for record

To split hairs, you cannot ever define a default constructor, because a default constructor is generated by the compiler when there are no constructors defined, thus any defined constructor is by definition not a default one.

If you want a record to have a no-arg constructor, records do allow adding extra constructors or factory methods, as long as the “canonical constructor” that takes all of the record fields as arguments is called.

public record Record(int recordId) {
   public Record() {
      this(0); 
   }
}

Leave a Comment