Learn Java
import com.learnjava.oopadvanced.Records;

Records in Java

@Concept

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.

@HowItWorks
1

The declaration

record Point(int x, int y) {} declares both the record's fields and its constructor's parameters in a single line.

2

Automatically generated pieces

A constructor, accessor methods like x(), not getX(), and correct equals(), hashCode(), and toString() implementations all come for free.

3

Implicit immutability

Every field in a record is implicitly final, and there's no way to reassign one after construction.

4

Records are implicitly final

A record class can never be extended by another class.

5

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.

@Example
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);
}
}
Output
x: 3, y: 4
Point[x=3, y=4]
1new Point(3, 4) uses the automatically generated constructor, setting both fields from the given arguments.
2p.x() and p.y() call the automatically generated accessor methods, named directly after the fields, not getX() or getY().
3Printing p directly calls its automatically generated toString(), which formats the record's name and every field's current value.
4Both println calls run once each, showing the manually-read values and the record's own generated string form.
@UseCases
Bundling a handful of related values, like coordinates or a date range, into an immutable, transparent carrier
Replacing a hand-written class whose only job was fields, a constructor, getters, and equals/hashCode/toString
Returning multiple values from a method by grouping them into a small record instead of an array or a full class
Recognizing when a class's purpose is best served by a record instead of a full mutable class

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.