Records for data classes
One line replaces 30+ lines of boilerplate for immutable data carriers.
Code Comparison
✕ Java 8
public class Point {
private final int x, y;
public Point(int x, int y) { ... }
public int getX() { return x; }
public int getY() { return y; }
// equals, hashCode, toString
}
✓ Java 16+
public record Point(int x, int y) {}
Why the modern way wins
One-line definition
A single line replaces constructor, getters, equals, hashCode, toString.
Immutable by default
All fields are final — no setter footguns.
Pattern-friendly
Records work with destructuring patterns in switch and instanceof.
Old Approach
Verbose POJO
Modern Approach
record
Since JDK
16
Difficulty
beginner
JDK Support
Records for data classes
Available
Widely available since JDK 16 (March 2021)
How it works
Records automatically generate the constructor, accessors (x(), y()), equals(), hashCode(), and toString(). They are immutable by design and ideal for DTOs, value objects, and pattern matching.