Quick revision
The short answer
Since Java 22, one case label may list several patterns, such as case Circle _, Square _ -> ..., but only if none of them declares a named binding. With names, the case body could not know which variables exist, because only one of the patterns actually matched. Unnamed patterns let us group subtypes of a sealed type into one branch while the switch stays exhaustive and no unused names appear.
What to remember
In this answer, we saw that unnamed patterns let one case label list several type patterns, as in case Circle _, Square _ -> ..., which removes duplicated branches over a sealed type. Named bindings are not allowed in such labels because only one pattern matches at run time, so no named variable would be definitely assigned in the body. The grouped case still counts toward exhaustiveness, so we keep the compile-time check that a default branch would lose.
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.
Since Java 22, one case label may list several patterns, such as case Circle _, Square _ -> ..., but only if none of them declares a named binding. With names, the case body could not know which variables exist, because only one of the patterns actually matched. Unnamed patterns let us group subtypes of a sealed type into one branch while the switch stays exhaustive and no unused names appear.
Why this question is asked
Switches over sealed types often have several subtypes that should be handled the same way. Before Java 22, we had to write one case per subtype with an identical body, or fall back to a default, which gave up the compiler's exhaustiveness check. Interviewers ask about this because it shows whether we understand how pattern bindings are scoped, and why the language allows grouping only in a specific form. In this answer, we will recap pattern bindings, see the duplication problem, show the grouped case, explain why named bindings are not allowed, and see how exhaustiveness still works.
1A Quick Recap of Bindings in Pattern Cases
A type pattern such as Circle c tests a value's type and, when it matches, introduces a binding: a new variable c of type Circle that the case body can use. The binding exists only where the pattern is known to have matched, which is the case body.
An unnamed pattern variable, written Circle _, still tests the type but introduces no binding. It became final in Java 22 through JEP 456. We use it when the type is all we care about, for example when every Circle and every Square is described the same way.
2The Duplication Problem
Here is a switch over a sealed Shape where three shapes share a description. Before Java 22 the only exhaustive way to write it was to repeat the body.
The names c, q, t and h are never used, and the three angular branches repeat themselves. Replacing them with default -> "angular" would be shorter, but then adding a new subtype such as Ellipse would silently be called angular, and the compiler would say nothing.
3Grouping Subtypes in One Case
With unnamed patterns we can list several patterns in one case label, separated by commas.
This reads as exactly what we mean: these three types are handled together. The switch has no default, so it is still checked for exhaustiveness. If someone adds Ellipse to the permits list, family fails to compile until Ellipse gets a case, which is the protection the default would have taken away.
4Why Named Bindings Are Not Allowed
Try the same thing with names and the compiler refuses.
| Case label | Value was a Square | Value was a Triangle |
|---|---|---|
| case Square q, Triangle t | q bound, t unassigned | t bound, q unassigned |
| case Square _, Triangle _ | nothing bound | nothing bound |
Think about what the body would mean. At run time the value is either a Square or a Triangle, never both. If it was a Square, q is bound and t has no value; if it was a Triangle, the opposite. Java requires every variable to be definitely assigned before it is read, and here neither q nor t can be proven assigned in the body. The language designers could have invented special rules, but any of them would make the body confusing. So the rule is simple: multiple patterns in one label are allowed only when none of them binds a name, and then there is nothing in scope to be confused about.
Notice that the exact wording of the error message varies between JDK versions; the rule itself is stable. And if we really need data from the value in a grouped branch, we can use the selector variable s, which is in scope everywhere, or a common accessor declared on the sealed interface.
5Exhaustiveness Still Works
Grouped unnamed patterns count toward coverage exactly as separate cases would. The compiler adds each listed type to the set of covered types, then checks that the sealed interface's permitted subtypes are all present. That means we get the brevity of a default branch together with the safety of naming every type.
The same works with record patterns, as long as no name is bound anywhere in them. For example, case Square(_), Hexagon(_) -> "regular polygon" is allowed. A guard can be added after the list, but since there are no bindings, it can only use variables from outside the switch or the selector itself.
6Follow-up questions
Can we mix named and unnamed patterns in one label?
No. If any pattern in a label with several patterns declares a named binding, the label is rejected. All of the patterns must be unnamed, written as Type _ or as record patterns whose components are all unnamed.
Is case Circle _, Square _ different from case Circle, Square for enums?
Yes. For enums, case RED, GREEN has always grouped constants, because constants bind nothing. Unnamed patterns extend the same grouping to type patterns, which do normally bind something.
Which Java version do we need?
Unnamed variables and patterns were previewed in Java 21 and became final in Java 22 through JEP 456, so on Java 25 they are standard. On Java 21 they need --enable-preview.
7Conclusion
In this answer, we saw that unnamed patterns let one case label list several type patterns, as in case Circle _, Square _ -> ..., which removes duplicated branches over a sealed type. Named bindings are not allowed in such labels because only one pattern matches at run time, so no named variable would be definitely assigned in the body. The grouped case still counts toward exhaustiveness, so we keep the compile-time check that a default branch would lose.
Open with
Unnamed patterns let me group several subtypes in one case, like case Square _, Triangle _, Hexagon _ -> "angular". The rule is that a case label can have multiple patterns only if none of them declares a named binding.
Then, if they want more
The reason is scoping: at run time the value matched exactly one of those patterns, so if I wrote case Square q, Triangle t, then in the body either q or t would have a value but never both, and Java's definite assignment rules cannot allow reading either one.
Rather than invent confusing rules, the language just requires all patterns in such a label to be unnamed, so nothing is in scope to confuse.
The big benefit is with sealed types: before this, I had to repeat identical bodies for each subtype or use default, and default gives up exhaustiveness, so adding a new subtype would silently fall into it.
With grouped unnamed patterns, the switch still names every permitted type, so it is still exhaustive, and adding a subtype breaks the build until I handle it. If I need data from the value in a grouped branch, I can use the selector variable or a method on the sealed interface. This is final since Java 22 through JEP 456.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 21, 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 "Explain how unnamed patterns help in switch cases over sealed types, for example case...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/26f17e7c63/