Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes enums and switches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enums and switches" Watch "enums and switches" New topic

enums and switches

Dan Doyle

Joined: Oct 02, 2006
Posts: 8
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 ( 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: an enum switch case label must be the unqualified name of an enumeration constant
case Coin.nickel: return CoinColor.nickel;
^ 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?

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059

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.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dan Doyle

Joined: Oct 02, 2006
Posts: 8
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.

sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
The java documentation for java 5's enums located here:

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 ]
I agree. Here's the link:
subject: enums and switches
It's not a secret anymore!