Records in Java
A record, introduced in Java 16, is a compact way to declare a class whose entire purpose is holding a fixed set of values — record Point(int x, int y) {} in one line generates a full class with private final fields for x and y, a constructor that sets both, public accessor methods named x() and y() (not getX() or getY()), and correctly-implemented equals(), hashCode(), and toString() methods, all without writing any of that boilerplate by hand. Records are implicitly final, meaning a record can't be extended, and every field is implicitly final too, since a record's whole design is built around being an immutable holder of data, not something that changes after creation.
Before records existed, a class meant only to bundle a few related values together still needed the same lengthy manual boilerplate as any full-featured class — fields, a constructor, getters, and overridden equals, hashCode, and toString, typically auto-generated by an IDE but still present as real code to maintain. Records exist specifically to remove that ceremony for the common case where a class really is just a transparent carrier for a fixed set of values.
The declaration
record Point(int x, int y) {} declares both the record's fields and its constructor's parameters in a single line.
Automatically generated pieces
A constructor, accessor methods like x(), not getX(), and correct equals(), hashCode(), and toString() implementations all come for free.
Implicit immutability
Every field in a record is implicitly final, and there's no way to reassign one after construction.
Records are implicitly final
A record class can never be extended by another class.
When to reach for one
Anywhere a class's whole job is to hold a small, fixed group of related values together, with no need for mutation.
public class RecordDemo { record Point(int x, int y) {} public static void main(String[] args) { Point p = new Point(3, 4); System.out.println("x: " + p.x() + ", y: " + p.y()); System.out.println(p); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.