In a switch statement, execution 'falls through' from one case to the next, unless the case ends with a break statement. A common programming error is to forget to insert a break at the end of a case.

End each case with a break statement or, if execution is supposed to fall through to the next case, comment the last line of the case with the following comment: /* falls through */

Such comments are not required for a completely empty case that is supposed to share the same implementation with the subsequent case.

In the following example, the PING case is missing a break statement. As a result, after reply is assigned the value of Message.PONG, execution falls through to the TIMEOUT case. Then the value of reply is erroneously assigned the value of Message.PING. To fix this, insert break; at the end of the PING case.

  • J. Bloch and N. Gafter, Java Puzzlers: Traps, Pitfalls, and Corner Cases, Puzzle 23. Addison-Wesley, 2005.
  • Code Conventions for the Java Programming Language: 7.8 switch Statements.
  • Help - Eclipse Platform: Java Compiler Errors/Warnings Preferences.