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?

0

2 Answers 2

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: // ... } 
Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to. You can do:

import static package.MyEnum.CONST1; private MyEnum var = CONST1; 

2 Comments

However, in switch I can write just CONST1. Why is that?
Because in the switch the type is known.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.