More information about records, including descriptions of the implicitly declared methods synthesized by the compiler, can be found in section 8.10 of The Java Language Specification.
A record class is a shallowly immutable, transparent carrier for a fixed set of values, called the record components. The Java language provides concise syntax for declaring record classes, whereby the record components are declared in the record header. The list of record components declared in the record header form the record descriptor.
A record class has the following mandated members: a canonical constructor, which must provide at least as much access as the record class and whose descriptor is the same as the record descriptor; a private final field corresponding to each component, whose name and type are the same as that of the component; a public accessor method corresponding to each component, whose name and return type are the same as that of the component. If not explicitly declared in the body of the record, implicit implementations for these members are provided.
The implicit declaration of the canonical constructor has the same accessibility
as the record class and initializes the component fields from the corresponding
constructor arguments. The implicit declaration of the accessor methods returns
the value of the corresponding component field. The implicit declaration of the
Object#equals(Object), Object#hashCode(), and Object#toString()
methods are derived from all of the component fields.
The primary reasons to provide an explicit declaration for the canonical constructor or accessor methods are to validate constructor arguments, perform defensive copies on mutable components, or normalize groups of components (such as reducing a rational number to lowest terms.)
For all record classes, the following invariant must hold: if a record R's
components are c1, c2, ... cn, then if a record instance is copied
as follows:
R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
API Note
A record class that implements java. is said
to be a serializable record. Serializable records are serialized and
deserialized differently than ordinary serializable objects. During
deserialization the record's canonical constructor is invoked to construct
the record object. Certain serialization-related methods, such as readObject
and writeObject, are ignored for serializable records. More information about
serializable records can be found in the
Java Object Serialization Specification, Section 1.13,
"Serialization of Records".
API Note
A record class structure can be obtained at runtime via reflection.
See Class#isRecord() and Class#getRecordComponents() for more details.
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract boolean | Returns: true if this record is equal to the
argument; false otherwise.the reference object with which to compare. obj)Overrides java. Indicates whether some other object is "equal to" this one. |
| public abstract int | Returns: a hash code value for this record.Overrides java. Returns a hash code value for the record. |
| public abstract String | Returns: a string representation of the object.Overrides java. Returns a string representation of the record. |
| Record | back to summary |
|---|---|
| protected Record() Constructor for record classes to call. | |
| equals | back to summary |
|---|---|
| public abstract boolean equals(Object obj) Overrides java. Indicates whether some other object is "equal to" this one. In addition
to the general contract of
R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Implementation Specification The implicitly provided implementation returns
Apart from the semantics described above, the precise algorithm used in the implicitly provided implementation is unspecified and is subject to change. The implementation may or may not use calls to the particular methods listed, and may or may not perform comparisons in the order of component declaration.
| |
| hashCode | back to summary |
|---|---|
| public abstract int hashCode() Overrides java. Returns a hash code value for the record.
Obeys the general contract of Implementation Specification The implicitly provided implementation returns a hash code value derived
by combining appropriate hashes from each component.
The precise algorithm used in the implicitly provided implementation
is unspecified and is subject to change within the above limits.
The resulting integer need not remain consistent from one
execution of an application to another execution of the same
application, even if the hashes of the component values were to
remain consistent in this way. Also, a component of primitive
type may contribute its bits to the hash code differently than
the
| |
| toString | back to summary |
|---|---|
| public abstract String toString() Overrides java. Returns a string representation of the record.
In accordance with the general contract of In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves. Implementation Specification The implicitly provided implementation returns a string which contains the name of the record class, the names of components of the record, and string representations of component values, so as to fulfill the contract of this method. The precise format produced by this implicitly provided implementation is subject to change, so the present syntax should not be parsed by applications to recover record component values.
| |