• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

enums and switches

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to figure out how to use enum values in a switch as cases. I am a little confused as I thought I had to prefix or qualify the enum value with the enum type. Here is an example I found from Joshua Bloch in 2003 (http://java.sun.com/features/2003/05/bloch_qa.html) which is rather old so I didn't necessarily expect it to work as the spec was finalized, but since I couldn't get my own code to work, I figured I would try this as it is what I thought I needed to do.

public enum Coin {
penny(1), nickel(5), dime(10), quarter(25);

Coin(int value) { this.value = value; }

private final int value;

public int value() { return value; }
}

public class CoinTest {
public static void main(String[] args) {
for (Coin c : Coin.VALUES)
System.out.println(c + ": \t"
+ c.value() +"� \t" + color(c));
}
private enum CoinColor { copper, nickel, silver }
private static CoinColor color(Coin c) {
switch(c) {
case Coin.penny: return CoinColor.copper;
case Coin.nickel: return CoinColor.nickel;
case Coin.dime:
case Coin.quarter: return CoinColor.silver;
default: throw new AssertionError("Unknown coin: " + c);
}
}
}

But this does not work. The compiler throws these errors for the syntax for each case statement:
CoinTest.java:11: an enum switch case label must be the unqualified name of an enumeration constant
case Coin.nickel: return CoinColor.nickel;
^
CoinTest.java:11: duplicate case label
case Coin.nickel: return CoinColor.nickel;
^
If I remove the qualifier/prefix(i.e. Coin), then the code compiles and runs. Well, almost, VALUES needs to be changed to values() in he main method.

So, I am a little confused. Why does the switch get to buck the trend of having to qualify the enum value with the enum type? Are there other instances in which this occurs? Is this a case of Java not being consistant or is it that switches are smart? In other words, once you use a variable reference to be switched on, the switch knows the type and only allows values of that type in the case statements. I didn't expect this since it seems everywhere else I use enums in my code, I have to do the full qualification. It made me think that the values were static, but I tested that and that is not the case either.

Any comments?

Dan
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi, Dan. Welcome to JavaRanch. I don't know the reasoning that you're looking for, but thought I'd drop in a "helpful hint" about using "code" tags to maintain the formatting thus making your code more readable. They are among the buttons under the textbox where you type your stuff. Someone will drop by soon, I'm sure, with some input related to your question.
 
Dan Doyle
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip.

I am curious if there are other special cases when an enum value can be used without specifying the enum type. I thought this was strange when I stumbled across it. I am reading the Sun Certified Programmer for Java 5 by Kathy Sierra and I didn't see anywhere in the book that highlights this instance and I picked up incorrectly that enums need to be qualified with their type. I suspect this is because switches are smart, which I didn't guess at. I would have thought that the switch would do a .equals() with the cases if they are enums, which should return false if the types being compared are different. I didn't know if someone might have a better explanation or could confirm my hunch about the switch statement.

Dan
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java documentation for java 5's enums located here:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

doesn't qualify the enum values with the class name when using them in a switch statement.

I suspect this is because switches are smart, which I didn't guess at. I would have thought that the switch would do a .equals() with the cases if they are enums


The switch expression, e.g.

has to evaluate to a char, byte, short, or int type, which are numeric types. Therefore, switch cases are compared to numeric types, so I don't think an enum's equals() method figures into it.
[ October 03, 2006: Message edited by: sven studde ]
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic