Sunday 28 September 2008

Do you know? Enum in Switch statements

You can now use an enum type in a "switch" statement, Earlier the switch expression had to be an int type.
Sun's Javac says -"enum switch case label must be the unqualified name of an enumeration constant.
So let's understand this with a simple example:

package com.doyou.know;

public class EnumInSwitch {

public static enum
Month { Jan, Feb, March }

public void printMonth(Month month) {

switch(month) {
case(Month.Jan): //Wrong: can not use parenthesis
System.out.println("January month..");

case Month.Feb: //Wrong Too: Enum reference can not be qualified
System.out.println("February month..");

case March: //RIGHT : this is how you do it ...
System.out.println("March month...");
}
}

public static void main(String[] args) {
new EnumInSwitch().printMonth(Month.March);
}
}


To summarize:

• In case statement the enum must be used without brackets.
• In case only the unqualified enum name (Jan, Feb, March) must be used.

7 comments:

Anonymous said...

I'm impressed, I have to admit. Seldom do I encounter a blog that'ѕ both eԁuсatіve and engaging,
аnd wіthout a doubt, you have hit the nаіl оn the head.

Thе іssue is an issuе thаt not enough folks аre sрeaking intеlligently
about. І am verу happy thаt I stumbled acrοss this
during my hunt for ѕomеthіng regarding this.


Heгe is my wеb-sіtе ... %anchoг_text%

Anonymous said...

Hi theгe tο еνery bodу, іt's my first go to see of this web site; this web site carries remarkable and truly fine information in support of readers.

Here is my web page :: sfgate.com
My web site - http://connect.webwaponline.com/link/2292

Anonymous said...

I'd like to find out more? I'd love to find out more details.


Here is my page sentimentalize

Anonymous said...

What a information of un-ambiguity and preserveness of valuable
experience about unexpected feelings.

Have a look at my weblog - hotmail email account

Anonymous said...

I do believe all the concepts you've offered to your post. They're very
convincing and will certainly work. Nonetheless, the posts are
very short for novices. Could you please extend them a bit from subsequent time?
Thank you for the post.

my web site - Www.Hollywoodstarshoney.com

Anonymous said...

I’vе been suгfing аround οnlinе more than ѕix hrѕ nοw, аnd after seеing you on http://ϳ2ееfolks.
blogsрot.cоm/, ӏ definіtеly
hаven't encountered any informative posts like yours. Honestly, I think that if more bloggers and siteowners introduced content like yours, the web would be even more helpful than it already is.

Feel free to visit my website; portable pizza oven

Anonymous said...

What's up, just wanted to tell you, I liked this post. It was practical. Keep on posting!

My homepage - nokia e5 specification