Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java record patterns

Reference for record patterns (Java 21): deconstruct records in instanceof and switch, nest patterns, use var, add guards and match sealed hierarchies.

A record pattern tests whether a value is an instance of a record class and, if so, extracts its components into variables in one step. It was finalized in Java 21 (JEP 440) and builds on records and pattern matching for instanceof and switch.

Instead of writing if (o instanceof Point p) { int x = p.x(); int y = p.y(); ... } you write if (o instanceof Point(int x, int y)) { ... }. Patterns can be nested, so you can take apart a record that contains other records in a single expression, and they work in switch with case labels, which makes handling a sealed hierarchy of records concise and exhaustive.

Java
if (obj instanceof Point(int x, int y)) {
    System.out.println(x + y);
}

Try it

Java instanceof and nested patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java switch over a sealed hierarchy with guards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Rules of the syntax

Form Meaning
Point(int x, int y) Matches a Point; x and y get the component values. The types must fit the component types exactly (a primitive component needs the same primitive or var).
Point(var x, var y) Components typed by inference.
Line(Point(var a, var b), var to) Nested pattern: the from component must itself be a Point.
Named<?>(String s, var v) Generic records: type arguments may be inferred or given as wildcards.
case Point(int x, int y) when x > 0 A guard on a switch label.

Usage notes

  • Matching calls the accessors. The pattern invokes the record's component accessor methods, in declaration order. If an accessor throws, the exception is wrapped in a MatchException.
  • null: a record pattern never matches null, and a nested pattern does not match a null component unless the component's pattern is a type pattern that covers its declared type (var x or Point p). In the first example new Line(null, ...) does not match Line(Point(...), ...) because Point(...) is a record pattern; it falls through to "something else".
  • Exhaustive switches: with a sealed interface and its record implementations, the compiler checks that your patterns cover every case, so no default is needed, and adding a new subtype later is a compile error until handled.
  • Dominance: put specific patterns before general ones; a later label that is already covered is a compile error.
  • Scope: pattern variables exist only where the match is known to have succeeded (&& chains and the if block, or the switch arm).
  • Only records. Ordinary classes cannot be deconstructed (a feature for custom deconstruction is not part of Java 25).
  • History: preview in Java 19 and 20; final in Java 21. The enhanced for loop form (for (Point(var x, var y) : list)) was dropped before the final release.
  • Prefer it over casts and accessor chains when branching on the shape of data; use plain accessors when you need only one component.