How do you use an enum in a Java switch statement, and what conveniences does switching on an enum give you?
answer
- case uses bare constant name, no Type. prefix
- compiler knows the type -> exhaustiveness warnings
- classic switch falls through -> need break/return
- default that throws catches new constants
- arrow form (14+): no fall-through, can be exhaustive
basics
~20 sPut the enum value in switch(value) and use the bare constant names (like MONDAY, not Day.MONDAY) in each case. The compiler knows the type, so you skip the prefix and it can warn if a case is missing.
solid answer
~40 sYou switch on the enum variable and write each case with the unqualified constant name, because the compiler already knows the enum type from the switch expression. This is cleaner than int or String constants: there are no magic numbers, typos become compile errors, and the case labels are checked against the actual enum. Tools and modern compilers can warn when a switch over an enum does not handle every constant (exhaustiveness). A traditional switch still falls through unless you break, so add break (or return) per case, and keep a default for safety, especially since new constants can be added later. Switch expressions (Java 14+) with the arrow form remove fall-through and can be made exhaustive without a default.
code
java · 10 linesenum Day { MONDAY, TUESDAY, WEDNESDAY }
String plan(Day d) {
// arrow switch expression: no fall-through, exhaustive
return switch (d) {
case MONDAY -> "start the week";
case TUESDAY, WEDNESDAY -> "midweek";
// no default needed if all constants covered
};
}go deeper
Knows you switch on the enum value and use bare constant names, and that you need break in the classic form.
Explains the type-safety/exhaustiveness benefits over int/String constants and uses a throwing default to catch new constants.
Contrasts classic vs arrow switch expressions, discusses exhaustiveness enforcement and the null NPE pitfall, and chooses the form deliberately.
Sets team conventions (prefer exhaustive switch expressions, no default when all constants covered so new constants force a compile error), and weighs switch vs polymorphic dispatch on the enum itself for maintainability.
## What an enum is An **enum** (enumeration) in Java is a special class whose instances are a fixed, named set of constants, for example `enum Day { MONDAY, TUESDAY, WEDNESDAY }`. Each constant (`Day.MONDAY`) is a singleton object of that type. Because the set is fixed and known at compile time, the compiler can reason about it. ## What a switch is A **switch** is a control-flow construct that picks one branch based on the value of an expression. The classic (statement) form looks like: ```java switch (expression) { case LABEL: ... break; default: ...; } ``` Each `case` lists a value to match; `break` stops execution; without `break` control **falls through** into the next case. ## Switching on an enum When the switch expression is an enum, the `case` labels must be the **unqualified constant names** — `case MONDAY:` not `case Day.MONDAY:`. This is a special rule: the compiler already knows the type of the switch expression is `Day`, so it resolves `MONDAY` inside that enum's namespace. Writing the prefix is a compile error in the classic form. ```java String plan(Day d) { switch (d) { case MONDAY: return "start the week"; case TUESDAY: return "keep going"; default: return "unknown"; } } ``` ## Why this beats int/String constants Before enums, people used `static final int` constants or strings. Those have problems: any int is accepted (no type safety), typos in strings fail silently at runtime, and the compiler cannot tell you that you forgot a value. Switching on an enum gives **type safety** (only valid constants compile), **no magic numbers**, and **exhaustiveness analysis** — the compiler/IDE can warn when a case for some constant is missing. ## Fall-through and default The classic switch still falls through, so each branch normally ends with `break` or `return`. Keep a `default` branch to handle constants you did not list — important because someone may add a new enum constant later and your switch would otherwise silently do nothing for it. A common pattern: handle every known constant explicitly and put `throw new IllegalStateException("Unexpected: " + d)` in `default` so a newly added constant fails loudly during testing. ## Switch expressions (Java 14+) The newer **arrow form** `case MONDAY -> ...;` has no fall-through (no `break` needed) and can be used as an expression that returns a value: ```java String plan = switch (d) { case MONDAY -> "start the week"; case TUESDAY, WEDNESDAY -> "midweek"; default -> "other"; }; ``` When a switch **expression** over an enum covers all constants (or has a default), the compiler enforces exhaustiveness, so adding a new constant later can turn into a compile error rather than a silent bug. ## One gotcha If the switched enum reference is `null`, the classic switch throws `NullPointerException` (it dereferences the value to read its ordinal). Guard against null before switching, or rely on newer pattern-matching switch which can have a `case null`.
- Why must case labels omit the enum type prefix?The switch expression's type is already the enum, so the compiler resolves the label within that enum's namespace; writing Day.MONDAY is a compile error in the classic switch form.
- What happens if the enum reference passed to a classic switch is null?It throws NullPointerException, because the switch reads the value's ordinal. Guard for null first, or use a pattern-matching switch with a case null.
Like a vending machine with labeled buttons: the buttons are fixed and named, so you press a label (MONDAY) not a raw number, and the machine can tell you if you forgot to wire up a button.
saying these in an interview costs you the question
- Writing case Day.MONDAY: in a classic switch (it does not compile).
- Forgetting break in the classic form and getting unintended fall-through.
- Assuming a missing default is always fine — a later-added constant silently does nothing.
- Believing switch on null is safe; it throws NPE in the classic form.