Records for data classes
One line replaces 30+ lines of boilerplate for immutable data carriers.
Porównanie kodu
✕ 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) {}
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
Verbose POJO
Nowoczesne podejście
record
Od JDK
16
Poziom trudności
Początkujący
Wsparcie JDK
Records for data classes
Dostępne
Widely available since JDK 16 (March 2021)
Jak to działa
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.
Powiązana dokumentacja