JLS 8.10.1. Record Components says:A record declaration implicitly creates instance fields, which are private and final. These are called record "components".
and JLS 8.10.3. Record Members says:The record components of a record class, if any, are specified in the header of a record declaration.
For each record component, a record class has a field with the same name as the record component and the same type as the declared type of the record component. This field, which is declared implicitly, is known as a component field.
And Questions explain
Why?Constructors and methods
1...
2...
3. If you write a constructor in a record explicitly then you must provide the canonical constructor as well.
JLS 8.10.4. Record Constructor Declarations says:
Can't you compile a record class with non-canonical constructor and no explicitly declared canonical constructor as following?If a canonical constructor is not explicitly declared in the declaration of a record class R, then a canonical constructor r is implicitly declared in R with the following properties:...
Code: Select all
public record Student(int id, String name){
public Student(){ //non-canonical constructor
this(10, "");
}
}