-2

I am implementing a simple switch case which will switch on an Enum value. Following is the code

ScheduleType scheduleType = ScheduleType.valueOf(scheduleTypeString);

switch (scheduleType) {

       case ScheduleType.CRON_EXPRESSION: 
                    System.out.println("Cron");
                    break;
}

But I am getting the following error in my IDE:

The qualified case label ScheduleType.CRON_EXPRESSION must be replaced with the unqualified enum constant CRON_EXPRESSION

Can somebody explain why do I get this error and what's wrong with the code. I know the right way to do is to remove the ClassName, but why do I need to do that? Because generally in comparisons I do use it like for example in equals and all. Thanks

10
  • 8
    The error tells you exactly what you have to do! Commented Apr 2, 2014 at 20:08
  • 3
    The compiler can't get friendlier than this! Commented Apr 2, 2014 at 20:08
  • 3
    @SambhavSharma They are probably sympathy votes and frankly the upvoters should explain their rationale here. I don't mean to be a jerk but this is not really a good question. The compiler error tells you exactly what to do to fix this. I downvoted it because 'it does not show any research effort'. Commented Apr 2, 2014 at 20:14
  • 2
    @SambhavSharma my answer touches on the why part. Commented Apr 2, 2014 at 20:18
  • 1
    I learned something from the question, I didn't know the info that Ajay posted. Commented Apr 2, 2014 at 20:20

2 Answers 2

7

Leave off the class name. case ScheduleType.CRON_EXPRESSION: should be case CRON_EXPRESSION: instead.

Or in other words, ScheduleType.CRON_EXPRESSION must be replaced with the unqualified enum constant CRON_EXPRESSION.

Sign up to request clarification or add additional context in comments.

Comments

3

Mike has explained the how part.

I shall attempt to explain the why part.

The switch case would make sense only if you compare enums of the same type. Comparing enums of type E1 with that of E2 doesn't make sense.

There was a bug which was raised to request for this feature which touches upon this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.