Community
Participate
Working Groups
The following code shouldn't produce a compiler error "This method must return a result of type String": 1: public class SwitchTest { 2: enum ABC {A, B, C; } 3: 4: String switchABC(ABC abc) { 5: switch (abc) { 6: case A: 7: return "A"; 8: case B: 9: return "B"; 10: case C: 11: return "C"; 12: } 13: } 14: } Reason: In method switchABC all possible cases are covered. Workaround: Introduce line 12a: return "" but this line can never be reached.
This code doesn't compile with javac 1.5, 1.6 and 1.7. An extra return statement is required. Closing as INVALID.
Sorry. Of course you're right. I should have made the check myself. Interestingly enough, the following method doesn't need an extra return at the end. (BTW, the default statement isn't reachable either. But this is another issue ;-) 1: public class SwitchTest { 2: enum ABC {A, B, C; } 3: 4: String switchABC(ABC abc) { 5: switch (abc) { 6: case A: 7: return "A"; 8: case B: 9: return "B"; 10: case C: 11: return "C"; 12: default: 13: throw new RuntimeException("not reachable"); 14: } 15: } I'll check the cases against the JLS and file a bug/enhancement req against javac...