Quick revision
The short answer
We can write it in one expression: return o instanceof Line(Point(var x1, var y1), Point(var x2, var y2)) && x1 == x2; The record pattern tests that o is a Line, deconstructs it into its two Points by calling the accessors, deconstructs each Point into its coordinates, and binds them so we can compare the x values. Record patterns are final since Java 21 (JEP 440).
What to remember
In this answer, we wrote a vertical-line check with nested record patterns: o instanceof Line(Point(var x1, var y1), Point(var x2, var y2)) && x1 == x2. The pattern tests the type, deconstructs each record by calling its accessors, matches the nested patterns and binds the coordinates, with var inferring int. Nested record patterns never match null, so a Line with a null point returns false instead of throwing. The same patterns work in switch cases with guards.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We can write it in one expression: return o instanceof Line(Point(var x1, var y1), Point(var x2, var y2)) && x1 == x2; The record pattern tests that o is a Line, deconstructs it into its two Points by calling the accessors, deconstructs each Point into its coordinates, and binds them so we can compare the x values. Record patterns are final since Java 21 (JEP 440).
Why this question is asked
Code that inspects nested data, such as a message containing an address containing a postcode, used to need a chain of instanceof checks, casts and getter calls. Record patterns let us describe the shape we expect and pull the values out in one step. Interviewers ask this small exercise because it shows quickly whether we know the syntax, whether we understand what the pattern does at run time, and whether we know how it treats null. In this answer, we will recap records and patterns, write the method the old way and the new way, explain what happens step by step when the pattern runs, and look at the null case and a few variations.
1A Quick Recap of Records and Record Patterns
A record is a class whose job is to carry data. Writing record Point(int x, int y) gives us a final class with two private final fields, a constructor, equals, hashCode, toString and two accessor methods named x() and y(). The list in parentheses is called the record's components.
A record pattern is the reverse of the constructor. Where new Point(1, 2) builds a Point from its components, the pattern Point(var x, var y) takes a Point apart into its components. Inside the parentheses we put further patterns, one per component. The keyword var asks the compiler to infer the component's type, so var x here is an int. Patterns can be nested, which means a component pattern can itself be a record pattern.
2The Method Before Record Patterns
Before Java 21 we would write the check with a type pattern and accessor calls. It works, but the shape of the data is hidden inside the calls.
Notice the explicit null checks. Without them, a Line whose a component is null would throw NullPointerException at line.a().x().
3The Method With Nested Record Patterns
Now the same check with record patterns. The pattern describes exactly the shape we want: a Line made of two Points.
- Is o a Line?YesCall a() and b()NoPattern fails: false
- Is a() a non-null Point?YesCall x() and y() on itNoPattern fails: false
- Is b() a non-null Point?YesCall x() and y() on itNoPattern fails: false
- Bind x1, y1, x2, y2all four are ints
- Evaluate x1 == x2true only for vertical lines
5 steps in order. Play them, or step through them one at a time.
Each nested pattern is tested only after the outer one has matched.
The bindings x1, y1, x2 and y2 are only in scope where the match is known to have succeeded, which is why we can use x1 and x2 on the right of && but not in an else branch. Since Java 22, when y1 and y2 are not needed, we can write the unnamed pattern _ in their place: Line(Point(var x1, _), Point(var x2, _)).
4How the Pattern Matches, Step by Step
When the instanceof runs, Java first tests whether o is a Line. If it is, it deconstructs the Line by calling its accessor methods, a() and b(), not by reading the fields directly. The reason is that a record's accessors are its public view of the data, and the pattern uses the same view any other caller would.
Each component value is then matched against its nested pattern. Point(var x1, var y1) asks whether the first component is a Point, and if so calls x() and y() on it and binds the results. The same happens for the second component. Only when every nested pattern succeeds does the whole pattern succeed and the bindings become available.
Here is the important detail about null. A nested record pattern never matches null. So for new Line(null, p), the first component is null, Point(var x1, var y1) does not match it, the whole instanceof is false, and we get false without any exception. That is the null check from the old version, done for us. By contrast, a lone var pattern or a type pattern used as a nested component, such as Line(var a, var b), does match a null component, because it accepts whatever the component holds.
5The Same Check in a Switch
Record patterns work in switch cases too, which is handy when we need several shapes. A guard with when adds the comparison.
Notice that the guarded cases come before case Line l, because an unguarded Line pattern would dominate them. The unnamed pattern _ in each case keeps only the coordinates we actually compare.
6Follow-up questions
Why does the pattern call accessors instead of reading fields?
Because the accessor is the record's API. A record can override an accessor, for example to return a defensive copy, and the pattern respects that. It also means an overridden accessor with side effects runs during matching, so accessors should be kept simple.
Do we need explicit types instead of var?
No, but we can use them. Line(Point(int x1, int y1), Point(int x2, int y2)) is equally valid. var is shorter and keeps the pattern readable when the types are obvious from the record declaration.
Which Java version do we need?
Record patterns were previewed in Java 19 and 20 and became final in Java 21 through JEP 440. The unnamed pattern _ became final in Java 22 through JEP 456. On Java 25, the current long-term release, both are standard features.
7Conclusion
In this answer, we wrote a vertical-line check with nested record patterns: o instanceof Line(Point(var x1, var y1), Point(var x2, var y2)) && x1 == x2. The pattern tests the type, deconstructs each record by calling its accessors, matches the nested patterns and binds the coordinates, with var inferring int. Nested record patterns never match null, so a Line with a null point returns false instead of throwing. The same patterns work in switch cases with guards.
Open with
I would write it as a single expression: return o instanceof Line(Point(var x1, var y1), Point(var x2, var y2)) && x1 == x2. The record pattern first checks that o is a Line, then deconstructs it by calling the accessor methods a() and b(), not by touching the fields.
Then, if they want more
Each component is matched against a nested pattern, Point(var x1, var y1), which again checks the type and calls x() and y(). var infers the component types, so x1 and x2 are ints, and the bindings are only in scope where the match succeeded, which is why I can use them after the &&.
One detail I would point out is null handling: a nested record pattern does not match null, so a Line with a null point simply gives false instead of a NullPointerException, which replaces the explicit null checks I would have written before Java 21.
If I do not need the y values, on Java 22 and later I can use the unnamed pattern underscore instead of var y1. Record patterns became final in Java 21 with JEP 440, and they work in switch cases too, where I can add a guard like when x1 == x2.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 22, Java 19, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Given record Point(int x, int y) and record Line(Point a, Point b), write a method that...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/869a949f17/