Java Records, Sealed Classes and Pattern Matching
For most of its history, modeling data in Java meant writing boilerplate: fields, constructors, getters, equals, hashCode, toString - repeated for every simple data holder. Modeling a closed set of alternatives meant enums or brittle class hierarchies. Recent Java versions changed this with three features that work best together: records, sealed classes, and pattern matching. Individually they are convenient; combined they let you model data as data and have the compiler verify you handled every possibility.
Records: data as data
A record is a transparent, immutable carrier for a fixed set of values. You declare the components once and the compiler generates the constructor, accessors, equals, hashCode, and toString. The point is not just less typing - it is a clear declaration of intent: this type is its data and nothing more. Two records with equal components are equal, which is exactly what you want for value types.
public record Point(int x, int y) {}
// Equivalent to a final class with two final fields,
// a canonical constructor, x()/y() accessors, and
// value-based equals, hashCode, and toString.
var a = new Point(1, 2);
var b = new Point(1, 2);
System.out.println(a.equals(b)); // trueSealed classes: closed hierarchies
A sealed class or interface restricts which types may extend or implement it. Instead of an open hierarchy that anyone can subclass, you declare the permitted subtypes explicitly. This turns a hierarchy into a closed, known set - the object-oriented equivalent of an enum whose cases carry their own data.
public sealed interface Shape
permits Circle, Rectangle {}
public record Circle(double radius) implements Shape {}
public record Rectangle(double w, double h)
implements Shape {}
// Shape now has exactly two possible forms,
// and the compiler knows it.Pattern matching: acting on shape
Pattern matching lets you test a value's type and destructure it in one step, without casting. Combined with switch, you can branch on which subtype you have and pull out its components directly. Record patterns go further, destructuring a record into its parts inline.
double area(Shape shape) {
return switch (shape) {
case Circle(double r) -> Math.PI * r * r;
case Rectangle(double w, double h) -> w * h;
};
}Why the three belong together
The real power emerges from the combination. Because the interface is sealed, the compiler knows the complete list of subtypes. Because you switch over them with pattern matching, the compiler can check exhaustiveness - if you add a new subtype and forget to handle it, the code stops compiling. You get the safety of a closed algebraic data type in ordinary Java.
| Feature | Gives you | Without it |
|---|---|---|
| Record | Concise, value-based data types | Boilerplate-heavy data classes |
| Sealed | A known, closed set of subtypes | Open hierarchies anyone can extend |
| Pattern matching | Type-safe branching + destructuring | instanceof chains and casts |
| All three | Compiler-checked exhaustiveness | Runtime surprises when a case is missed |
Where to use it - and where not to
- Use records for DTOs, value objects, and results where identity is the data itself.
- Use sealed hierarchies to model a fixed set of variants - payment methods, message types, parse results.
- Reach for pattern-matching switches when behavior genuinely differs by variant and you want exhaustiveness.
- Avoid records for entities with identity and lifecycle, or for mutable objects - they are the wrong fit.
Conclusion
Records, sealed classes, and pattern matching move Java toward expressing data models directly, with less ceremony and more compiler support. Records say what a value is, sealing says what forms it can take, and pattern matching acts on those forms with exhaustiveness checked at compile time. Used together they eliminate a class of runtime bugs by turning 'did I handle every case' from a code review question into a compiler guarantee.