4

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?

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

2 Answers2

5

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:
  // ...
}
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
1

You don't have to. You can do:

import static package.MyEnum.CONST1;

private MyEnum var = CONST1;
Stewart
  • 17,616
  • 8
  • 52
  • 80