• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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;
}
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic