Java Records Explained: Less Boilerplate Data Classes (Beginner Guide)
Learn Java records: how one line replaces constructor, getters, equals, hashCode, and toString. What records give you, when to use them, validation, and the rules to know.
If you've ever written a Java class that just holds data — and groaned at the dozens of lines of constructor, getters, equals, hashCode, and toString — records are about to make your life much better. A record collapses all that boilerplate into a single line. This guide shows you exactly what records are, when to use them, and the small print worth knowing.
The problem records solve
Consider a simple class to hold a 2D point. The "traditional" version is painfully long:
// the OLD way — ~30 lines of boilerplate class Point { private final int x, y; Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } @Override public boolean equals(Object o) { /* ... */ } @Override public int hashCode() { /* ... */ } @Override public String toString() { /* ... */ } }
The record way — one line
record Point(int x, int y) { }
That's it. This one line automatically generates: a constructor, accessor methods (x() and y()), equals(), hashCode(), and a readable toString(). Watch:
Point p = new Point(3, 4); p.x(); // 3 (accessor, note: x() not getX()) p.toString(); // "Point[x=3, y=4]" — auto-generated Point q = new Point(3, 4); p.equals(q); // true — compares by content, for free
What records give you (and their rules)
- Immutable by default — record fields are
final; you can't change them after creation. (Great for safe, predictable data — see immutability.) - Content-based equality —
equals()andhashCode()compare the actual values, so they work perfectly as HashMap keys (no need to hand-write them, unlike the equals/hashCode pain). - Concise accessors —
point.x(), notpoint.getX(). - A clean toString — perfect for logging and debugging.
When to use a record (and when not to)
| Use a record for… | Use a normal class for… |
|---|---|
| DTOs (data transfer objects) | objects with mutable state |
| API request/response bodies | rich behaviour & logic |
| coordinates, ranges, key-value pairs | entities that need inheritance |
| immutable value objects | JPA @Entity classes* |
* JPA entities need a no-arg constructor and mutability, so they're usually normal classes, not records.
Adding validation (compact constructor)
You can still add logic — like validation — using a "compact constructor":
record Point(int x, int y) { Point { // compact constructor — runs on creation if (x < 0 || y < 0) throw new IllegalArgumentException("No negatives"); } }
Records are one of my favourite modern-Java quality-of-life features — they made a whole category of tedious "data holder" classes vanish from my code. The rule of thumb I use: if a class exists mainly to carry data and shouldn't change after creation, make it a record. DTOs, API payloads, coordinate pairs, config values — all perfect fits. Keep normal classes for things with real behaviour or mutable state. Once you start reaching for records, you'll wonder how you tolerated writing all that boilerplate before. It's a small feature that makes everyday Java noticeably nicer.