Elliott C. Back: In Aere Aedificare

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, nbsp, languages. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

21 Responses to 'Enumerated Constants in Java'

  1. NIHAR RANJAN said:

    on February 12th, 2006 at 10:49 pm

    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 said:

    on May 9th, 2006 at 10:35 pm

    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 said:

    on June 6th, 2006 at 8:38 pm

    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 said:

    on June 9th, 2006 at 6:37 am

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

    Best regards,
    Mattias

  5. Tommy Driffers said:

    on July 27th, 2006 at 8:01 am

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

  6. Omen 666 said:

    on July 27th, 2006 at 8:02 am

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

  7. Yaba said:

    on August 29th, 2006 at 4:00 pm

    Thanks a lot. Just stumbled over this.

  8. Matt Brotherson said:

    on October 19th, 2006 at 10:11 am

    Many thanks for your post!

    Cheers,
    -Matt

  9. Ton-Yun Fang said:

    on April 6th, 2007 at 6:44 pm

    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 said:

    on May 19th, 2007 at 7:59 am

    Gracias!, muy útil.

  11. Del.irio.us said:

    on July 4th, 2007 at 6:22 am

    Thanks a lot
    Gracias

  12. bagley said:

    on July 6th, 2007 at 6:35 am

    thx

  13. Sébastien said:

    on July 11th, 2007 at 7:38 am

    Hi,

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

    Have a nice day.

  14. Nate said:

    on August 11th, 2007 at 3:52 pm

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

  15. Khan said:

    on September 12th, 2007 at 11:10 am

    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 said:

    on April 11th, 2008 at 1:59 pm

    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 said:

    on May 26th, 2008 at 12:52 pm

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

  18. John said:

    on June 4th, 2008 at 10:03 am

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

  19. Ville Anttonen said:

    on August 29th, 2008 at 5:13 am

    Thanks! Helped me alot..

  20. Sleeveen said:

    on September 10th, 2008 at 1:58 pm

    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. Anna Skawińska said:

    on September 16th, 2008 at 3:32 am

    Thanks a lot!

Your Thoughts Go Here:

Powered by WP Hashcash