• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

switch and enums

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have an enum define like
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

i tried to use it with switch like this:

1 switch (Day.FRIDAY) {
2 case Day.FRIDAY:
3 System.out.println("Friday!");
4 break;
5 }

I have an error at line 2 that says:
The enum constant Day.FRIDAY reference cannot be qualified in a case label.

My questionis why?
...i can use FRIDAY and it's ok!

Regards
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salut Andrei

try this :



Regards M
 
Andrei Nistor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still not working that way...

I found something that says:

" It inherits the context from the type of the object in the switch() statement, and that cases can simply use unqualified names."

..but still fog ... i think this is strange behavior on case-enums.

Regards
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When testing an enum in a switch statement, you don't prefix the values in the case clauses with the name of the enum. The switch statement already knows that you are testing for enum values. So the code should read:

Day day = Day.SUNDAY;
switch (day) {
case SUNDAY: System.out.println("Friday!");
break;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic