Enumerated Constants in Java
Java 1.5 introduces this great feature long present in other languages called an enumerated constant. In theory, it lets you replace ugly java code like this:
public static final int PANCAKE = 0; public static final int ORANGES = 0; public static final int CEREAL = 0; public static final int MILK = 0;
With a simple one line definition:
public enum breakfast { pancake, oranges, cereal, milk };
This is good because it improves the readability and maintability of the code. Enums are also strictly more powerful than a list of integers, as they are actually a shorthand for a class, and contain methods and fields of their own. However, transitioning to using java 1.5 is not as easy as you think. I wanted to use an enum in a switch / case statement, but was having trouble. My first incorrect try was to fully qualify the enum:
switch(visited.operator){
case BinaryExpression.Operator.and: ... break;
}
This gives the following error:
The enum constant BinaryExpression.Operator.and reference cannot be qualified in a case label
Searching for tutorials online didn’t help–the only one I could find that was truly useful was this one from InformIT. I finally discovered that it inherits the context from the type of the object in the switch() statement, and that cases can simply use unqualified names. The code now becomes:
switch (visited.operator) {
case and: ... break;
}
One you get used to them, enums in Java 1.5 are great. They really should have been there all along!
| This entry was posted on Thursday, March 3rd, 2005 at 5:09 pm and is tagged with cereal milk, case label, case statement, switch case, switch statement, informit, java code, java java, integers, using java, shorthand, oranges, readability, constants, languages. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback. |
27 Responses to “Enumerated Constants in Java”
Leave a Reply
very good article.teaches complex things in simple and less steps.very clear,good understandable thn its contemporaries. does Elliott Back plan to write a full volume java book with its advanced version ? greatly welcome if he does so.
Thanks for the simple and to-the-point fix. The “[reference] cannot be qualified in a case label” error was driving me crazy..
I wonder why this happens… Is Java treating the enum constant as a ’static’ field when it is referred to by its full name?
Thanks,
Tommy
Cool.. Thanks for the article. Saved me time for sure.
Btw, weird choice import the namespace to the switch isn’t it? anyway, I’m just guessing here..
[]s
Leonardo
Just wanted to thank you for solving the problem for me. It was driving me crazy
Best regards,
Mattias
Awesome!! Gee THANKS!!
and Ohh please give us more elaboration… thanx a lot
LOL all of your gago!!!
HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA
HEHEHEHEHEHEEHHEEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHE
****!!!!!!!
Thanks a lot. Just stumbled over this.
Many thanks for your post!
Cheers,
-Matt
Thanks so much for figuring this out! The error was driving me nuts because I KNEW you could do enums in Java 1.5….
Gracias!, muy útil.
Thanks a lot
Gracias
thx
Hi,
Thanks for the tip, I was just looking to the same stuff with the switch
)
Have a nice day.
YAPWTY (yet another person who thanks you) for the helpful tip!
-nate
Thanks so much for this one. I was going crazy trying to figure this out. Your site helped a lot.
However, I wonder why they did it this way – it does become a bit confusing. Wouldn’t it be easier to use the FQN, that way a programmer would have some ideas about which options are available from the Enum? But now I have to first use the FQN to figure out the option and then delete the first part of it make sure syntax is correct – kind of odd.
Anyway, thanks again.
Thank you very much, the ONLY site that could help me were yours! ^^
Sometimes Java drives us all mad… I would have used the FQN instead of this.
Ivan
Man, I could have spent hours on this. Thanks a bunch
Thanks for the wonderful advice. You’ve saved me a ton of time.
Thanks! Helped me alot..
Likewise, a big thank you!
I’m updating a large chunk of code to Java 1.6 and was having exactly this problem – a slightly better error message would solve the problem e.g. Use an unqualified reference in this case. Instead of Enum.CONSTANT just use CONSTANT.
You don’t have to know everything…. you just have to know where to look
Thanks a lot!
Thanks for the information.
I also found this other post, with a link to a bug report with an explanation.
http://www.davestone.net/blog/post/The-enum-con...
thanks so much! this helped me fix my “The enum constant ___ reference cannot be qualified in a case label” error really fast =]
this helped me so much!! thx
Thank you Elliot, and thank you google. Saved me 15 ~ 20 minutes figuring this out.
Thank you for your help!
[...] on this article, Java 1.5 make it very easy to create enumerated constants. The keyword “enum” is a new [...]