• 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

enums

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// 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. ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ");
}
}
}
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shyam thanks alot this was wonderful explanation
 
Sheriff
Posts: 9707
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
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...
 
Shyam Sakalley
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of 3 if any one passing value 47 or 67 or may be anything that cause a problem
 
Shyam Sakalley
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
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
Hmm...Shyam to the rescue....

Good one Shyam
 
reply
    Bookmark Topic Watch Topic
  • New Topic