Class RecordPatternExpr

All Implemented Interfaces:
NodeWithFinalModifier<RecordPatternExpr>, NodeWithModifiers<RecordPatternExpr>, NodeWithRange<Node>, NodeWithTokenRange<Node>, NodeWithType<PatternExpr, Type>, Observable, Visitable, HasParentNode<Node>, Cloneable

public class RecordPatternExpr extends PatternExpr implements NodeWithFinalModifier<RecordPatternExpr>

Record Patterns

Record patterns were officially added in Java 21 to allow the deconstruction of record values and provide convenient access to inner fields through pattern matching.

JDK 21 Grammar

Pattern
    TypePattern
    RecordPattern

RecordPattern:
    ReferenceType ( [PatternList] )

PatternList
    Pattern {, Pattern }

Example

Example taken from JEP440: RecordPatterns
 record Pair(Object x, Object y) {}

Pair p = new Pair(42, 42);

if (p instanceof Pair(String s, String t)) {
    System.out.println(s + ", " + t);
} else {
    System.out.println("Not a pair of strings");
}
See Also: