Quick revision
The short answer
We write int price = switch (size) { case S -> 10; case M -> 12; case L -> 15; }; and need no default, because a switch expression that covers every constant is exhaustive. If XL is added later, recompiling this code fails with an error that the switch does not cover all values, which points us to every switch to update. If old compiled code meets XL at run time, the switch throws MatchException on Java 21 and later, and IncompatibleClassChangeError on some older versions.
What to remember
In this answer, we wrote a switch expression over Size that covers S, M and L and needs no default, because covering every constant makes it exhaustive. Adding XL makes the code fail to compile when it is rebuilt, which lists every switch that needs a decision, while old compiled code that meets XL throws MatchException on Java 21 and later, or IncompatibleClassChangeError from older compilers. We also saw that case labels use bare names, that Java 21 allows qualified names, and why a default branch would hide the new constant.
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 write int price = switch (size) { case S -> 10; case M -> 12; case L -> 15; }; and need no default, because a switch expression that covers every constant is exhaustive. If XL is added later, recompiling this code fails with an error that the switch does not cover all values, which points us to every switch to update. If old compiled code meets XL at run time, the switch throws MatchException on Java 21 and later, and IncompatibleClassChangeError on some older versions.
Background to this question
This small exercise checks whether we know modern switch syntax, and more importantly whether we understand exhaustiveness: what the compiler checks, what it generates, and what happens when an enum changes after the code was compiled. That last part matters in real systems where enums live in shared libraries. In this answer, we will write the switch expression, see why no default is needed, add XL and look at the compile error, then look at the run-time failure in code that was not recompiled, and finish with the syntax rules for case labels.
1A Quick Recap: Switch Expressions
Since Java 14, switch can be an expression: it produces a value that we can assign or return. With the arrow form, case X -> value, each case yields its value directly, and there is no fall-through from one case to the next, so no break statements. Because an expression must always produce a value, the compiler requires a switch expression to be exhaustive: every possible input must be handled by some case.
2Writing the Switch
For an enum, exhaustive means every constant appears in some case. When that is true, a default branch is unnecessary.
Notice that no default is needed and none is wanted. The compiler can see all three constants are covered, so the expression always produces a value. A null selector throws NullPointerException, as it always has for switches on enums, unless we add an explicit case null, which is allowed since Java 21.
Several constants can share a result, written case S, M -> 10, and a case can run a block that produces its value with yield, such as case L -> { log(size); yield 15; }.
3Adding XL: The Compile Error Is the Feature
Now someone adds a new size.
- Is the code using the switch recompiled?YesCompile error at every switch missing XLNoOld bytecode still runs
- Does old code receive XL at run time?YesMatchException from the switchNoNothing happens yet
- Fix: add case XL -> 18 and redeploythe compiler confirms every switch is complete
3 steps in order. Play them, or step through them one at a time.
Recompiled code fails early and safely; old binaries fail only when the new value actually arrives.
This error is exactly what we want. Every switch expression over Size that does not handle XL fails to compile, so the build gives us a complete list of the places where a decision about XL is needed. If the original code had included a default branch, such as default -> 0, the new constant would have compiled silently and every XL item would have cost 0. The missing default is what turns a business bug into a build error.
4When Old Compiled Code Meets XL
In a single project, everything is recompiled together, so the compile error catches everything. But if Size lives in a shared library, another service may still run bytecode compiled against the old three-constant version, with the new library on its class path or with XL arriving in data from another service. Its switch has no case for XL. Because the switch expression must produce a value, the compiler had already added a hidden default that throws, and that is what fires.
So the failure is loud rather than silent: the code throws instead of inventing a price. In recent JDKs the exception is MatchException, introduced with pattern matching in Java 21; code compiled by older compilers used IncompatibleClassChangeError for the same situation. Which one we see depends on the compiler version that produced the bytecode, so check your version. Either way, it is better than a wrong price, and it is why teams that share enums across services also need a policy for unknown values at the boundaries.
5Case Label Syntax
Inside a switch on an enum, case labels traditionally use the bare constant name, case S, not case Size.S, because the selector's type already tells the compiler which enum the names belong to. Since Java 21, qualified names such as case Size.S are also accepted, which helps when a switch over a sealed interface mixes enum constants from different enums.
6Follow-up questions
Does the old switch statement with case S: and break also check exhaustiveness?
No. A classic switch statement over an enum compiles even if it misses constants; a missing constant simply does nothing. Only switch expressions, and switch statements that use patterns or case null, must be exhaustive. That is a strong reason to prefer switch expressions for logic that must handle every constant.
Should we add default -> throw new IllegalStateException() to be safe?
It does not add safety, because the compiler already inserts an equivalent throwing default, and it costs us the compile-time error when a constant is added. Without a default, adding XL fails the build; with it, the problem waits for run time.
What happens with case null in an enum switch?
Since Java 21, we can write case null -> 0 to handle a null selector explicitly instead of getting a NullPointerException. It can also be combined with default as case null, default ->, though that brings back the downsides of a default.
7Conclusion
In this answer, we wrote a switch expression over Size that covers S, M and L and needs no default, because covering every constant makes it exhaustive. Adding XL makes the code fail to compile when it is rebuilt, which lists every switch that needs a decision, while old compiled code that meets XL throws MatchException on Java 21 and later, or IncompatibleClassChangeError from older compilers. We also saw that case labels use bare names, that Java 21 allows qualified names, and why a default branch would hide the new constant.
Open with
I would write int price equals switch on size, case S arrow 10, case M arrow 12, case L arrow 15, closed with a semicolon, with no default.
Then, if they want more
Because it is a switch expression it must produce a value for every input, and since all three constants are covered the compiler accepts it as exhaustive. A null size throws NullPointerException unless I add case null.
If someone later adds XL and the code is recompiled, it fails with an error that the switch expression does not cover all possible input values, and that is the point: the compiler lists every switch where someone must decide what XL costs. A default branch would have hidden that, and XL would silently get the default price.
If old bytecode that was compiled against the three-constant enum meets XL at run time, for example because the enum is in a shared library, the compiler-generated default throws, MatchException on Java 21 and later and IncompatibleClassChangeError from older compilers, so it fails loudly rather than computing a wrong price.
Case labels use the bare constant names, S rather than Size.S, though Java 21 also accepts the qualified form.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 14
- 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 "Write a switch expression over an enum Size { S, M, L } returning a price, without a...": https://scrivio-interview-library.netlify.app/java/enums/29566aac70/