J.VII · Java 25 practice question
A developer writes a switch expression over an enum with three constants and handles every constant explicitly. Why should the developer omit a default branch?
Check answer and explanation
Correct answer: Omitting default lets the compiler verify exhaustiveness if a new enum constant is added later
Explanation
Omitting default lets the compiler verify exhaustiveness if a new enum constant is added later, whereas a manual default branch masks missing cases and defeats compile-time exhaustiveness checking.
Common trap
Java 25 does allow a default branch in an enum switch expression even when all constants are covered, so claiming it causes a compile-time error is false. Bytecode optimization into tableswitch is unrelated to why manual default branches are discouraged for exhaustiveness.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.