JAVA.SWITCH.DEFAULT.POSITION
The default label is not at the beginning or end of a switch statement.
Vulnerability and risk
If the default label is not placed at the beginning or end of a switch statement, maintainability may be impaired, or incorrect coding may cause unintended behavior.
Mitigation and prevention
Place the default label at the beginning or end of the switch statement.
Example 1
Copy
public class C03 {
/* default located at first */
public void testOK1(int num) {
switch (num) {
default:
break;
case 1:
break;
case 2:
break;
}
}
/* default located at last */
public void testOK2(int num) {
switch (num) {
case 1:
break;
case 2:
break;
default:
break;
}
}
/* default located in the middle */
public void testNG1(int num) {
switch (num) {
case 1:
break;
default:
break;
case 2:
break;
}
}
}