GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Records for data classes | java.evolved

Home / Language / Records for data classes
Language Beginner

Records for data classes

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

One line replaces 30+ lines of boilerplate for immutable data carriers.

Records
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) {}
Share 𝕏
See a problem with this code? Let us know.
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.

Related Documentation
Records (JEP 395) ↗ Record class ↗
Proof
View proof source ↗
Related patterns
Language Intermediate

Flexible constructor bodies

Java 8
class Square extends Shape {
    Square(double side) {
        super(side, side);
        // can't validate BEFORE super!
        if (side <= 0)
            throw new IAE("bad");
    }
}
Java 25+
class Square extends Shape {
    Square(double side) {
        if (side <= 0)
            throw new IAE("bad");
        super(side, side);
    }
}
Hover to see modern ➜
JDK 25+ →
Language Intermediate

Private interface methods

Java 8
interface Logger {
    default void logInfo(String msg) {
        System.out.println(
            "[INFO] " + timestamp() + msg);
    }
    default void logWarn(String msg) {
        System.out.println(
            "[WARN] " + timestamp() + msg);
    }
}
Java 9+
interface Logger {
    private String format(String lvl, String msg) {
        return "[" + lvl + "] " + timestamp() + msg;
    }
    default void logInfo(String msg) {
        IO.println(format("INFO", msg));
    }
    default void logWarn(String msg) {
        IO.println(format("WARN", msg));
    }
}
Hover to see modern ➜
JDK 9+ →
Language Intermediate

Pattern matching in switch

Java 8
String format(Object obj) {
    if (obj instanceof Integer i)
        return "int: " + i;
    else if (obj instanceof Double d)
        return "double: " + d;
    else if (obj instanceof String s)
        return "str: " + s;
    return "unknown";
}
Java 21+
String format(Object obj) {
    return switch (obj) {
        case Integer i -> "int: " + i;
        case Double d  -> "double: " + d;
        case String s  -> "str: " + s;
        default        -> "unknown";
    };
}
Hover to see modern ➜
JDK 21+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.