ODNYX.

Java Records Explained

Video Tutorial Coming Soon — Channel Production Underway

A record is a special kind of class, introduced to cut down the boilerplate of writing plain data-holder classes by hand.

Tip: reach for a record whenever a class's only job is to hold a fixed set of values, like a coordinate pair or a simple DTO — you get a constructor, getters, equals(), hashCode(), and toString() for free.

The old way (a plain class)

public class Point {
    private final int x;
    private final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() { return x; }
    public int getY() { return y; }
    // ...plus equals(), hashCode(), toString() you'd normally have to write by hand
}

The record way

public record Point(int x, int y) {}

That single line gives you everything the class above had: a constructor, x() and y() accessor methods, equals(), hashCode(), and a readable toString().

Point p = new Point(3, 4);
System.out.println(p.x());     // 3
System.out.println(p);         // Point[x=3, y=4]

Records are immutable

Once created, a record’s fields can’t be changed — there are no setters. This makes records a natural fit for representing fixed, unchanging data.

What’s next

That’s a wrap on Class Types for now. More class types (like sealed classes) can be added here later as their own topics.

📋 Java Class Types Interview Questions (Enum, Record)