• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

New user: Enum with switch case

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have decided to appear for SCJP 6. And as a first unavoidable step, I have registered to Big Moose Saloon...

First of all, thank you all in advance.

Have one issue with Enum- switch case.

K&B has one example related to switch statement :



Can someone please explain the following:

1. How can we use 'red' directly in case statement without using enum name e.g. Color.red?

2. Why cant we use 'Color.red' in case statement?( It gives compile time error..)

Thanks
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hitesh! Welcome to javaranch.

when you use enums in a switch case, it acts like as if we have imported the enum. The enum variable in the switch expression, acts as the base for all the enum constants used as the case statements...

[Edit: added welcome message ]
 
Hitesh Rathod
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Thanks for the quick reply!!!

Good to know the answer. Why are such concepts not covered in K&B? Or have i missed it while reading?

Thanks again.
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Hitesh. K&B covers very few details about Enums. So, its upto you to make code snippets to explore the minute details of Enums. All the best.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain why the above code gives the output:

red green blue done

TIA
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't put a 'break' statement in, then once you enter the case, every line will be executed.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

solar wind wrote:Can someone explain why the above code gives the output:

red green blue done

TIA



Since there are no break statements, whenever a particular case matches..all the following case statemets are executed regardless of a match
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Himalay for explanation.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic