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.
A component field is private, final, and non-static.
A component field is annotated with the annotations, if any, that appear on the corresponding record component and whose annotation interfaces are applicable in the field declaration context, or in type contexts, or both (§9.7.4).
Furthermore, for each record component, a record class has a method with the same name as the record component and an empty formal parameter list. This method, which is declared explicitly or implicitly, is known as an accessor method.
If an accessor method for a record component is declared explicitly, then all of the following must be true, or a compile-time error occurs:
- The return type of the accessor method (§8.4.5) must be the same as the declared type of the record component.
- The accessor method must not be generic (§8.4.4).
- The accessor method must be a
publicinstance method with no formal parameters and nothrowsclause.
If a record class has a record component for which an accessor method is not declared explicitly, then an accessor method for that record component is declared implicitly, with the following properties:
- Its name is the same as the name of the record component.
- Its return type is the same as the declared type of the record component.
- It is not generic.
- It is a
publicinstance method with no formal parameters and nothrowsclause. - It is annotated with the annotations, if any, that appear on the corresponding record component and whose annotation interfaces are applicable in the method declaration context, or in type contexts, or both (§9.7.4).
- Its body returns the value of the corresponding component field.
The restrictions on record component names (§8.10.1) mean that no implicitly declared accessor method has a signature that is override-equivalent with a non-private method of class Object. An explicit method declaration that takes one of the restricted names, such as public void wait() {...}, is not an accessor method, since wait is never a record component name.
Annotations that appear on a record component are not propagated to an explicitly declared accessor method for that record component. In some situations, the programmer may need to duplicate a record component's annotations on an explicitly declared accessor method, but this is not generally necessary.
Annotations that are propagated to an implicitly declared accessor method must result in a legally annotated method. For example, in the following record declaration, the implicitly declared accessor method x() would be annotated with @SafeVarargs, but such an annotation is illegal on a fixed arity method (§9.6.4.7):
record BadRecord(@SafeVarargs int x) {} // Error
Record classes may explicitly declare instance methods other than accessor methods, but may not explicitly declare instance variables (§8.10.2). Explicit declarations of class methods and class variables are permitted.
All members of record classes, including implicitly declared members, are subject to the usual rules for member declarations in a class (§8.3, §8.4, §8.5).
All of the rules concerning inheritance that apply to normal classes apply to record classes. In particular, record classes may inherit members from superinterfaces, although a superinterface method will never be inherited as an accessor method because the record class will always declare, explicitly or implicitly, an accessor method that overrides the superinterface method.
For example, a record class can inherit default methods from its direct superinterfaces, although the default method bodies have no knowledge of the component fields of the record class. The following program prints Logged:
public class Test {
interface Logging {
default void logAction() {
System.out.println("Logged");
}
}
record Point(int i, int j) implements Logging {}
public static void main(String[] args) {
Point p = new Point(10, 20);
p.logAction();
}
}
A record class provides implementations of all the abstract methods declared in class Record. For each of the following methods, if a record class R does not explicitly declare a method with the same modifiers, name, and signature (§8.4.2), then the method is implicitly declared as follows:
-
A method
public final boolean equals(Object)that returnstrueif and only if the argument is an instance of R, and the current instance is equal to the argument instance at every record component of R; otherwisefalseis returned.Equality of an instance
aof a record class R with another instancebof the same record class at a record componentcis determined as follows:- If the type of the record component
cis a reference type, equality is determined as follows: if the value of the component fieldcof bothaandbis the null reference thentrueis returned; if the value of the component fieldcof eitheraorb, but not both, is the null reference thenfalseis returned; otherwise equality is determined by invoking theequalsmethod on the value of the component fieldcofa, with an argument that is the value of the component fieldcofb. - If the type of the record component
cis a primitive type T, equality is determined as if by invoking thestaticmethodcompareof the wrapper class corresponding to T (§5.1.7), with the first argument given by the value of the component fieldcofa, and the second argument given by the value of the component fieldcofb; if the method would return0thentrueis returned, otherwisefalseis returned.
The use of
comparein wrapper classes ensures that the implicitly declaredequalsmethod is reflexive and behaves consistently with the implicitly declaredhashCodemethod for record classes that have floating-point components. - If the type of the record component
-
A method
public final int hashCode()that returns a hash code value derived from the hash code values at every record component of R.The hash code value of an instance
aof a record class at a record componentcis as follows:- If the type of the record component
cis a reference type, then the hash code value is determined as if by invoking thehashCodemethod on the value of the component fieldcofa. - If the type of the record component
cis a primitive type T, then the hash code value is determined as if by subjecting the value of the component fieldcofato boxing conversion (§5.1.7) and then invoking the methodhashCodeof the wrapper class corresponding to T on the resulting object.
- If the type of the record component
-
A method
public final String toString()that returns a string derived from the name of the record class and the names and string representations of every record component of R.The string representation of a record component
cof an instanceaof a record class is as follows:- If the type of the record component
cis a reference type, then the string representation is determined as if by invoking thetoStringmethod on the value of the component fieldcofa. - If the type of the record component
cis a primitive type T, then the string representation is determined as if by subjecting the value of the component fieldcofato boxing conversion (§5.1.7) and then invoking the methodtoStringmethod of the wrapper class corresponding to T on the resulting object.
- If the type of the record component
Note that equality, hash code values, and string representations are determined by looking at the values of component fields directly, rather than by invoking accessor methods.
Consider a record class R that has components c_1, ..., c_n, and an implicitly declared accessor method for every component, and an implicitly declared equals method. If an instance r1 of R is copied in the following way:
R r2 = new R(r1.c1(), r1.c2(), ..., r1.cn());
then, assuming r1 is not the null reference, it is always the case that the expression r1.equals(r2) will evaluate to true. Explicitly declared accessor methods and equals methods should respect this invariant. It is not generally possible for a compiler to check whether explicitly declared methods respect the invariant. The following record declaration is bad style because its accessor methods clip the x and y components and therefore prevent p3 from being equals to p1:
record SmallPoint(int x, int y) {
public int x() { return this.x < 100 ? this.x : 100; }
public int y() { return this.y < 100 ? this.y : 100; }
public static void main(String[] args) {
SmallPoint p1 = new SmallPoint(200,300);
SmallPoint p2 = new SmallPoint(200,300);
System.out.println(p1.equals(p2)); // prints true
SmallPoint p3 = new SmallPoint(p1.x(), p1.y());
System.out.println(p1.equals(p3)); // prints false
}
}