I have to work with a library that defines an enum like this:
public static enum LibVal {
VAL_1,
VAL_2,
VAL_3;
}
I get this enum as a method argument:
public void libCallback(LibVal val){
//.... some implementation
}
Why does Java disallow the use of switch
with the LibVal
enum inside the libCallback
method? However, if the lib had declared its enum as non-static it would work. This is confusing as this SO-answer states, that there is really no difference...
Edit:
As bobkilla Stated: I tried LibVal.VAL_1 inside my switch, which should be allowed. I provide a code-sample which wouldn't work!
class TestClassForEnum {
public static enum TestEnum{ ONE, TWO; }
}
class WhichUsesEnumInsideMethod{
//completely unrelated to TestClassForEnum.TestEnum!!!
public static final int ONE = 0x1282
void doSomethingWithEnum(TestEnum e){
//here I cannot switch:
//FORBIDDEN BY JAVA
switch (e) {
case TestEnum.ONE:
//...
}
//Cannot USE EITHER, because ONE is a static final int inside this scope?!:
switch (e) {
case ONE:
//...
}
}