Quick revision
The short answer
A record pattern deconstructs a record by calling its accessor methods, so any code we put in an overridden accessor runs every time a pattern matches. A defensive copy in the accessor means a new copy on every match, a side effect means matching changes state, and if the accessor throws, the match fails with a MatchException that wraps the original exception. The safe design is to keep accessors simple and pure and do any copying in the constructor.
What to remember
In this answer, we saw that record patterns deconstruct records by calling their accessors, so an overridden accessor runs during every match. A defensive copy there allocates on every match, side effects happen at unpredictable points, and an exception from an accessor reaches us as a MatchException with the original as its cause. Doing validation and copying once in a compact constructor, and leaving accessors plain, keeps matching fast and predictable.
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.
A record pattern deconstructs a record by calling its accessor methods, so any code we put in an overridden accessor runs every time a pattern matches. A defensive copy in the accessor means a new copy on every match, a side effect means matching changes state, and if the accessor throws, the match fails with a MatchException that wraps the original exception. The safe design is to keep accessors simple and pure and do any copying in the constructor.
Why this question is asked
Records look like plain data, so we tend to forget that their accessors are ordinary methods we are allowed to override. Pattern matching makes that matter, because it calls those methods behind our back, sometimes many times in a hot path. Interviewers ask this because it tests whether we understand what a record pattern actually does at run time rather than treating it as field access. In this answer, we will recap records and their accessors, see what a pattern does with them, look at three ways an overridden accessor causes trouble, and then show the safe way to write the record.
1A Quick Recap of Record Accessors
For record Order(String id, List<String> items), the compiler generates two private final fields and two public methods, id() and items(), called accessors, that return those fields. We may write our own version of an accessor, which then replaces the generated one, as long as it has the same name, return type and is public.
A record pattern such as case Order(var id, var items) does not read the private fields. It calls id() and items() and matches the results against the nested patterns. This is a deliberate choice: the accessors are the record's public interface, and a pattern should see the record exactly as any other caller would. The consequence is that whatever our accessor does, the pattern does too.
2Problem 1: A Defensive Copy in the Accessor
A common habit carried over from JavaBeans is to protect a mutable list by copying it in the getter. In a record it looks like this.
Every successful or attempted deconstruction calls items(), and each call allocates a new ArrayList the size of the order. In a router that matches millions of orders per minute, that is millions of short-lived lists, extra garbage collection and slower matching, all for a copy nobody needed. Notice also that two cases deconstruct the same record; we should not assume the accessor is called only once per switch. The JLS leaves room for the compiler to avoid repeat calls, so the number of calls is not something to rely on in either direction (check the specification for your Java version).
3Problem 2: Side Effects During Matching
Some accessors do more than return a value: they log, count accesses, lazily load data, or update a cache. In a pattern match, those effects happen as part of testing a case, even a case that ends up not being chosen because its guard is false. A log line saying an order's items were read, printed for every routing decision, is confusing at best. A lazy load that hits the database inside an accessor turns a cheap switch into a network call. Matching code is supposed to be a question about the shape of data; side effects make it an action, and the order and count of those actions are not something we control.
4Problem 3: An Accessor That Throws
If an accessor throws during matching, Java does not let the exception escape as is. The match completes abruptly with a MatchException, and the original exception is attached as its cause.
4 messages in order. Play them, or step through them one at a time.
The original exception survives only as the cause.
So a catch block written for IllegalStateException around the switch does not catch it, because the exception type has changed. Code that unwrapped exceptions by type, or logged only the top-level class, now sees MatchException. The reason Java wraps it is to make clear that the failure happened during pattern matching rather than in the code of a case.
5The Safe Way: Copy in the Constructor
The fix for all three problems is the same: do the work once, when the record is created, and keep the accessors trivial. A compact constructor, the record constructor written without a parameter list, is the place for validation and copying.
Now the record is safe to share because its list is unmodifiable, and every pattern match calls a plain accessor that returns a field. Invalid states, such as the NaN temperature, are rejected in the constructor instead of exploding later inside a match. Notice that this is the general rule for records: they should be transparent carriers of their data, and the accessors should return what the constructor stored.
6Follow-up questions
Is it ever reasonable to override a record accessor?
Yes, for small, pure adjustments such as returning an unmodifiable view of an array component, or adding an annotation. The test is whether calling it a hundred times in a row has any effect beyond returning the same value cheaply. If not, it is fine for pattern matching.
What exactly is MatchException?
It is an unchecked exception added in Java 21 and thrown when pattern matching completes abruptly. Two common causes are an accessor that throws during record deconstruction, and an exhaustive switch meeting a value no case covers, such as a new sealed subtype compiled separately.
Does the same apply to classes that are not records?
Record patterns only work with records. For an ordinary class, we use a type pattern and call its getters ourselves, so we see and control each call.
7Conclusion
In this answer, we saw that record patterns deconstruct records by calling their accessors, so an overridden accessor runs during every match. A defensive copy there allocates on every match, side effects happen at unpredictable points, and an exception from an accessor reaches us as a MatchException with the original as its cause. Doing validation and copying once in a compact constructor, and leaving accessors plain, keeps matching fast and predictable.
Open with
Record patterns deconstruct a record by calling its accessor methods, not by reading the fields, so if a record overrides an accessor, that code runs during pattern matching.
Then, if they want more
That has three consequences. If the accessor makes a defensive copy, say new ArrayList of a list component, every match allocates a copy, which in a hot routing switch is a lot of garbage.
If the accessor has side effects, such as logging or lazy loading, those happen while we are only testing cases, even cases that end up not being chosen, and I should not rely on how many times the accessor is called.
And if the accessor throws, the match completes abruptly with a MatchException wrapping the original exception, so a catch for the original type will not see it.
My recommendation is to keep record accessors simple and pure, and do validation and copying once in the compact constructor, for example items = List.copyOf(items), which gives an unmodifiable list and means the accessor can just return the field. That keeps records as transparent data carriers, which is exactly what pattern matching assumes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- 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 "Record patterns deconstruct using accessor methods. What happens if a record overrides...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/35b93479a5/