Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Record class

Reference for java.lang.Record, the base class of every record: generated members, canonical constructors, equals and hashCode, reflection and restrictions.

java.lang.Record is the abstract base class of all record types, declared with the record keyword (final since Java 16). You never extend it yourself: the compiler makes every record implicitly extend Record, and declaring extends Record by hand is an error.

A record is a transparent, immutable carrier for a fixed group of values. From the header (the components) the compiler generates private final fields, a canonical constructor, accessor methods named like the components, and implementations of equals(), hashCode() and toString() that all look at every component.

Java
record Point(int x, int y) { }

Point p = new Point(3, 4);
System.out.println(p.x() + " " + p); // 3 Point[x=3, y=4]

Try it

Java What a record gives you

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.

Java Records and reflection

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

Java
public abstract class Record

Record itself declares only equals(Object), hashCode() and toString() (all abstract); every record gets concrete versions.

Part of a record declaration Description
record Name(Type a, Type b) { } Header with components. Records can be generic, nested or local.
Canonical constructor Generated automatically (Name(Type a, Type b)), or written explicitly.
Compact constructor Name { ... } with no parameter list: validate or normalize parameters, and the fields are assigned at the end.
Accessors a() and b() (not getA()). May be declared explicitly with the same name and the same return type.
Static members Allowed: fields, methods, nested types, initializers.
Instance methods Allowed. Instance fields beyond the components are not.
implements Allowed (interfaces such as Comparable, sealed interfaces).

Usage notes

  • Shallow immutability. The fields are final, but a component such as a List or array can still be mutated by its owner. Copy defensively in the compact constructor, e.g. List.copyOf.
  • Equality compares components with their own equals. Arrays compare by reference, so avoid array components or override equals/hashCode.
  • toString() format: Person[name=Ana, age=30, tags=[admin, dev]] for default output.
  • Restrictions. Records are implicitly final, cannot extend another class, cannot declare instance fields outside the header and cannot be abstract. Native methods are not allowed.
  • Serialization is safe: deserializing a record calls its canonical constructor, so validation cannot be bypassed.
  • Good uses: DTOs, map keys, return values with several parts, pattern-matching data (see record patterns), small value types.
  • Poor fits: JPA entities and anything needing mutation or a no-argument constructor.
  • Annotations on components propagate to the field, accessor, constructor parameter or record component depending on the annotation's @Target.
  • Since Java 16 records are a standard feature; they were previews in 14 and 15.