This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enums" Watch "enums" New topic
Author

enums

Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55
// Sample code.
public class TestEnum {
enum Color {red, green, blue};
public static void main(String[] args) {
Color c = Color.red;
switch(c){
case red: System.out.println("red ");
}
}
}

Here in switch block we are able to access enum 'red' with out reffering to its class 'Color' like Color.red (hope because of Color object that we pass to switch).

I want to know how actually switch work for enums.

Can any one give explanation?

Thanks in advance.

--
[ September 29, 2008: Message edited by: Venkata S. ]
Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
That's what I thought, you could pass in an enum object and have it's corresponding values as the case constants. And the reason that is probable maybe because the case in switch statements can take in any compile time constants and the enum values satisfy this condition.
Shyam Sakalley
Greenhorn

Joined: Oct 05, 2008
Posts: 23
Actualy it happend because

when we write this enum class like this
enum Color {red, green, blue}

then java complier includes internally like this

public class TestEnum {
static final class Color exteds Enum
{
public static final Color = red;
public static final Color = green;
public static final Color = blue;
static
{
red = new Color("red",0);
green = new Color("green",1);
blue = new Color("blue",2);
}
public static void main(String[] args) {
Color c = Color.red;
switch(c){
case red: System.out.println("red ");
}
}
}
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
shyam thanks alot this was wonderful explanation
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

nice explanation shyam. but your code has some erros, the constant declarations will be like this

public static final Color red;
public static final Color green;
public static final Color blue;

as you can see that there will be no = symbol...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shyam Sakalley
Greenhorn

Joined: Oct 05, 2008
Posts: 23
thanks i did mistake

but i want to give some enum related example plese pick up

package com.shyam.core1.enumtest;

public class PlayingCard {
public static final int SUIT_SPADES=0;
public static final int SUIT_HEARTS=1;
public static final int SUIT_CLUBS =2;
public static final int SUIT_DIAMONDS=3;

private int suit;
private int rank;

public PlayingCard(int suit,int rank)
{
this.suit=suit;
this.rank=rank;
}

public int getSuit()
{
return suit;

}
public String getSuitName()
{
String name="";
switch(suit)
{
case SUIT_SPADES:
name="spades";
break;
case SUIT_HEARTS:
name="hearts";
break;
case SUIT_CLUBS:
name="clubs";
break;
case SUIT_DIAMONDS:
name="diamond";
break;

default:
System.out.println("Invalid suit");


}
return name;

}
}


----------------------------------------------------

package com.shyam.core1.enummain;

import com.shyam.core1.enumtest.PlayingCard;

public class TestPlayingCard{

public static void main(String[] args) {
PlayingCard c = new PlayingCard(PlayingCard.SUIT_SPADES,1);
System.out.println("card is the "+ c.getSuit()+" of "+c.getSuitName());
PlayingCard c1 = new PlayingCard(3,2);
System.out.println("card is the "+ c1.getSuit()+" of "+c1.getSuitName());
}

}
-----------------------------

when we run above code then no problem but in future any one passing value like this
PlayingCard c1 = new PlayingCard(3,2);
that create problem that's why enum concepts came into existant
Shyam Sakalley
Greenhorn

Joined: Oct 05, 2008
Posts: 23
Instead of 3 if any one passing value 47 or 67 or may be anything that cause a problem
Shyam Sakalley
Greenhorn

Joined: Oct 05, 2008
Posts: 23
the solution of the above problem is we have to do restrict to the user means when user set this types of value like 47 or 67 then it should be compile time error so we will go to use of enm like this given below
----------------------------------------
package com.shyam.core1.enumtest;

public enum Suit {

SPADES,HEARTS,CLUBS,DIAMONDS

}
------------------------------------------

package com.shyam.core1.enumtest;

public class PlayingCard1 {
private Suit suit;
private int rank;

public PlayingCard1(Suit suit,int rank)
{
this.suit=suit;
this.rank=rank;
}

public Suit getSuit()
{
return suit;

}
public String getSuitName()
{
String name="";
switch(suit)
{
case SPADES:
name="spades";
break;
case HEARTS:
name="hearts";
break;
case CLUBS:
name="clubs";
break;
case DIAMONDS:
name="diamond";
break;

default:
System.out.println("Invalid suit");


}
return name;
}

}
------------------------------------------------------

import com.shyam.core1.enumtest.PlayingCard1;
import com.shyam.core1.enumtest.Suit;

public class TestPlayingCard1 {
public static void main(String[] args) {
PlayingCard1 c = new PlayingCard1(Suit.SPADES,1);
System.out.println("card is the "+ c.getSuit()+" of "+c.getSuitName());
//PlayingCard c1 = new PlayingCard(47,2);

}

}
------------------------------------------------------

in the above code this code //PlayingCard c1 = new PlayingCard(47,2); is not allowed it whold be compile time error
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Hmm...Shyam to the rescue....

Good one Shyam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enums