Java Records — Free Java Tutorial
Learn Java Records in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Java Records in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Java Records in Java
Records (Java 16+) are concise immutable data classes. A single record declaration automatically generates: constructor, private final fields, getters (accessor methods), equals(), hashCode(), and toString().
Records are immutable — all fields are final and set at construction time. They are ideal for DTOs (Data Transfer Objects), value objects, and data carriers.
Record components are accessed with component-name() methods (not getXxx()): for record Point(int x, int y), use point.x() and point.y().
Records can have: compact constructors (for validation), additional methods, static fields, and can implement interfaces. They cannot extend classes (they implicitly extend Record).
Java Records — Syntax
// Define a record
record Point(int x, int y) {}
// Automatically has:
// - public Point(int x, int y) constructor
// - public int x(), public int y() accessors
// - equals(), hashCode(), toString()
Point p = new Point(3, 4);
p.x(); // 3
p.y(); // 4
p.toString(); // "Point[x=3, y=
Learn Java Records step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →