I'm trying to declare an enum in Java and use a variable of that type in a switch statement, where all possible cases for enum constants of that type are covered.
enum MyEnum {
FOO,
BAR
}
private static void test(MyEnum e) {
String msg;
switch (e) {
case FOO:
msg = "foo";
break;
case BAR:
msg = "bar";
break;
}
System.out.println("Enum is: " + e + " msg is: " + msg); //compiler error
}
Why is the compiler not capable of detecting that this switch will always initialize msg
(or throw a NullPointerException because e
is null
)?