Elliott C. Back: Internet & Technology

Enumerated Constants in Java

Posted in Computers & Technology, Java by Elliott Back on March 3rd, 2005.

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”

  1. NIHAR RANJAN says:

    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.

  2. Tommy A says:

    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

  3. Leonardo Barbosa says:

    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

  4. Mattias says:

    Just wanted to thank you for solving the problem for me. It was driving me crazy :)

    Best regards,
    Mattias

  5. Awesome!! Gee THANKS!!
    and Ohh please give us more elaboration… thanx a lot
    LOL all of your gago!!!

  6. Omen 666 says:

    HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA
    HEHEHEHEHEHEEHHEEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHE
    ****!!!!!!!

  7. Yaba says:

    Thanks a lot. Just stumbled over this.

  8. Many thanks for your post!

    Cheers,
    -Matt

  9. Ton-Yun Fang says:

    Thanks so much for figuring this out! The error was driving me nuts because I KNEW you could do enums in Java 1.5….

  10. Rodrigo says:

    Gracias!, muy útil.

  11. Del.irio.us says:

    Thanks a lot
    Gracias

  12. bagley says:

    thx

  13. Sébastien says:

    Hi,

    Thanks for the tip, I was just looking to the same stuff with the switch :) )

    Have a nice day.

  14. Nate says:

    YAPWTY (yet another person who thanks you) for the helpful tip!
    -nate

  15. Khan says:

    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.

  16. Ivan says:

    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

  17. Michael says:

    Man, I could have spent hours on this. Thanks a bunch :-)

  18. John says:

    Thanks for the wonderful advice. You’ve saved me a ton of time.

  19. Ville Anttonen says:

    Thanks! Helped me alot..

  20. Sleeveen says:

    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 ;-)

  21. Feniks says:

    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...

  22. coder says:

    thanks so much! this helped me fix my “The enum constant ___ reference cannot be qualified in a case label” error really fast =]

  23. Jan says:

    this helped me so much!! thx

  24. Jack says:

    Thank you Elliot, and thank you google. Saved me 15 ~ 20 minutes figuring this out.

  25. Aaron S says:

    Thank you for your help!

  26. [...] on this article, Java 1.5 make it very easy to create enumerated constants. The keyword “enum” is a new [...]

Leave a Reply

Powered by WP Hashcash