☕ Java Advanced

What are Java records?

Answer

Records (Java 16, finalized from preview in 14) are a special class declaration designed for transparent, immutable data carriers. record Point(int x, int y) { } automatically generates: a canonical constructor, private final fields, public getters (x() and y()), and correct equals(), hashCode(), and toString() implementations. Records cannot extend other classes (they implicitly extend Record), cannot have non-static instance fields beyond the record components, and are implicitly final. You can add compact constructors for validation, custom methods, and static fields. Records are the Java equivalent of Kotlin data classes and are ideal for DTOs, value objects, and immutable data.