I have, say,
private enum MyEnum { CONST1, CONST2 }
I'm required to write
private MyEnum var = MyEnum.CONST1;
But in switch I shall write
switch(var) {
case CONST1:
...
}
Why this difference?
I have, say,
private enum MyEnum { CONST1, CONST2 }
I'm required to write
private MyEnum var = MyEnum.CONST1;
But in switch I shall write
switch(var) {
case CONST1:
...
}
Why this difference?
You're not required to write MyEnum.CONST1. You can use a static import on it and then you can refer to CONST1 without MyEnum.
The switch construct was provided as a convenience, so they have made it as convenient as possible, and don't require the name of the enum type. In addition, it makes is more obvious that you can use instances of only the one enum, and can't do this sort of thing:
switch (val) {
case MyEnum1.VAL1:
// ...
break;
case MyEnum2.VAL1:
// ...
}
You don't have to. You can do:
import static package.MyEnum.CONST1;
private MyEnum var = CONST1;