Java Reference ยท Keywords and Statements
Java records
Reference for Java records: compact immutable data classes with generated constructor, accessors, equals, hashCode and toString, compact constructors and rules.
A record is a concise class for carrying immutable data. You list the components in the header, and the compiler generates private final fields, a canonical constructor, accessor methods named after the components, and equals(), hashCode() and toString() based on all components.
Records were finalized in Java 16. They are shallowly immutable: the fields cannot be reassigned, but a component that references a mutable object still exposes that object's changes.
record Point(int x, int y) { } Point p = new Point(3, 4); p.x(); // 3
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
[modifiers] record Name<T>(Type1 comp1, Type2 comp2) [implements I] { // optional: compact constructor, extra constructors, methods, static members }
| Part | Rule |
|---|---|
| Components | Each becomes a private final field and a public accessor comp(). Varargs allowed as last component. |
| Canonical constructor | Generated unless you declare it. Compact form Name { ... } omits parameters and assigns fields at the end. |
equals/hashCode |
Component-wise. Floating components compare like Double.compare. |
toString |
Name[x=3, y=4] format. |
| Other members | Static fields and methods, instance methods, nested types. No instance fields beyond components. |
Usage notes
- Implicitly
final: a record cannot be extended and cannot extend a class (it extendsjava.lang.Record). It may implement interfaces. - Can be local, nested or top-level. Nested and local records are implicitly
static. - Validation: do it in the compact constructor and reassign parameters (like
values = List.copyOf(values)) to normalize. You cannot assignthis.xthere. - Accessors may be overridden but must be public and return the same type.
- Mutable components: copy them defensively; otherwise equality and hash codes can change while the record is in a
HashSet. - Annotations and generics on components work as on fields/parameters.
- Serialization: records serialize by their components and always call the canonical constructor, so validation applies.
- Not for everything: JPA entities and classes needing mutable state or inheritance are poor fits.
- Pattern matching: records support deconstruction in
instanceofandswitch, including nested patterns.
Related
class,enum,instanceof,sealed- Lessons: Records, Immutable classes, Pattern matching