JEP 405 allows record patterns, extending the pattern matching capabilities of instanceof operators, and switch expressions, to include record patterns that explicitly refer to the components of the record.
record Rectangle(int x, int y, int w, int h) {}
int area(Object o) {
if (o instanceof Rectangle(int x, int y, int w, int h)) {
return w * h;
}
return 0;
}
Such patterns can include nested patterns, where the components of records are themselves records, allowing patterns to match more object graphs.