-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

Sambhav Sharma
  • 5,741
  • 9
  • 53
  • 95
  • 8
    The error tells you _exactly_ what you have to do! – Dawood ibn Kareem Apr 02 '14 at 20:08
  • 3
    The compiler can't get friendlier than this! – adarshr Apr 02 '14 at 20:08
  • ScheduleType.CRON_EXPRESSION must be replaced with ... CRON_EXPRESSION, as it told you. – David Conrad Apr 02 '14 at 20:09
  • Okay, so I got quite a many vote downs for his, but then I got vote ups too.. not sure if I should take this down or not? – Sambhav Sharma Apr 02 '14 at 20:10
  • Well my gut says it is a ridiculous question and only if I was smart enough to understand that sentence. But I guess it happens to everyone some or the other time. I will take it down, but I do need to know why do I need to make that change? – Sambhav Sharma Apr 02 '14 at 20:13
  • 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'. – Radiodef Apr 02 '14 at 20:14
  • 2
    @SambhavSharma my answer touches on the why part. – Ajay George Apr 02 '14 at 20:18
  • Yep, thanks for the explaination.. :) – Sambhav Sharma Apr 02 '14 at 20:20
  • 1
    I learned something from the question, I didn't know the info that Ajay posted. – Mike B Apr 02 '14 at 20:20
  • 1
    It is, however, a duplicate of http://stackoverflow.com/q/10161408/1081110 and http://stackoverflow.com/q/2663980/1081110 and http://stackoverflow.com/q/14325481/1081110 so it should probably be closed. – Dawood ibn Kareem Apr 02 '14 at 20:42

2 Answers2

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.

Mike B
  • 5,390
  • 2
  • 23
  • 45
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.

Ajay George
  • 11,759
  • 1
  • 40
  • 48