This discussion is archived
14 Replies Latest reply: Aug 16, 2011 6:58 AM by 796440 RSS

Enum in switch case

860521 Newbie
Currently Being Moderated
public class Sample {
     public enum Dogs {
          lab, sheph, harrier;
     }

     public static void main(String[] args) {
          Sample.Dogs myDog = Sample.Dogs.sheph;
          switch (myDog) {
          case Sample.Dogs.lab:
               System.out.println("lab");
          case Sample.Dogs.sheph:
               System.out.println("Sheph");
          case Sample.Dogs.harrier:
               System.out.println("harrier");
          }
     }
}
When I try to compile this, I get the following errors:
Sample.java:9: an enum switch case label must be the unqualified name of an enumeration constant
                case Sample.Dogs.lab:
                                ^
Sample.java:11: an enum switch case label must be the unqualified name of an enumeration constant
                case Sample.Dogs.sheph:
                                ^
Sample.java:11: duplicate case label
                case Sample.Dogs.sheph:
                ^
Sample.java:13: an enum switch case label must be the unqualified name of an enumeration constant
                case Sample.Dogs.harrier:
                                ^
Sample.java:13: duplicate case label
                case Sample.Dogs.harrier:
                ^
5 errors
Could someone please explain
1) WHY I should not use fully qualified name of the enumeration constant?
2) Why do I see the error "duplicate case label" when I have declared the case labels only only once?
3) Why do I not see the "duplicate case label" error for the case label Sample.Dogs.lab?

Edited by: user10745151 on Aug 15, 2011 5:57 AM
  • 1. Re: Enum in switch case
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

    Then edit your post and format the code correctly.

    db
  • 2. Re: Enum in switch case
    YoungWinston Expert
    Currently Being Moderated
    user10745151 wrote:
    Could someone please explain
    1) WHY I should not use fully qualified name of the enumeration constant?
    Not exactly sure, but it's totally unnecessary since you have already 'qualified' the name by your
    Sample.Dogs myDog = Sample.Dogs.sheph;
    line.

    My advice: just do as it requires.
    2) Why do I see the error "duplicate case label" when I have declared the case labels only only once?
    Again, not sure; but I'll bet you dollars to doughnuts that it disappears when you actually use legal labels.
    3) Why do I not see the "duplicate case label" error for the case label Sample.Dogs.lab?
    Almost certainly because it's the first label encountered.

    Oh, and BTW, you should add 'break' statements after each 'println'.

    Winston
  • 3. Re: Enum in switch case
    875329 Newbie
    Currently Being Moderated
    I checked by copy-pasting, removing the full enumeration of the enums in the cases and the errors don't appear when I compile, so it must be that it simply doesn't like the full enumeration.
  • 4. Re: Enum in switch case
    Kayaman Guru
    Currently Being Moderated
    872326 wrote:
    I checked by copy-pasting, removing the full enumeration of the enums in the cases and the errors don't appear when I compile, so it must be that it simply doesn't like the full enumeration.
    Why would you want to use the full version anyways, that's like programmers using the FQN of every class they use.
  • 5. Re: Enum in switch case
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Kayaman wrote:
    Why would you want to use the full version anyways, that's like programmers using the FQN of every class they use.
    The question that came in my mind was: Why don't you use polymorphism prior select?

    bye
    TPD
  • 6. Re: Enum in switch case
    YoungWinston Expert
    Currently Being Moderated
    872326 wrote:
    ...so it must be that it simply doesn't like the full enumeration.
    And the [url http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11]JLS explains why. Check out specifically the requirement for 'EnumConstantName:' (which is what you're concerned with): it says that it must be an Identifier.
    That spec is in para [url http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8]3.8; I leave it for you to work out for yourself.

    Winston
  • 7. Re: Enum in switch case
    860521 Newbie
    Currently Being Moderated
    Thanks for the reply YoungWinston.
    My doubt is:
    Sample.Dogs.sheph
    is the fully qualified name of the identifier
    sheph
    why can the fully qualified name of the identifier not be used?

    Also, my question about the duplicate label error+ still remains

    Edited by: sharath on Aug 16, 2011 1:10 AM
  • 8. Re: Enum in switch case
    796440 Guru
    Currently Being Moderated
    sharath wrote:
    Sample.Dogs.sheph
    is the fully qualified name of the identifier
    sheph
    why can the fully qualified name of the identifier not be used?

    Also, my question about the duplicate label error+ still remains
    Why do you care? No matter what the answer is, it won't change how you write your Java code.

    There are two possibilities right now:

    1) You are writing your own Java compiler and hence you need to be concerned about language arcania.

    2) You're wasting your time on pointless trivialities when that time would be better spent getting acquainted with the language and core APIs by writing some code.
  • 9. Re: Enum in switch case
    EJP Guru
    Currently Being Moderated
    why can the fully qualified name of the identifier not be used?
    Because the grammar specified in the Java Language Specification doesn't permit it, because of a choice made by the language designers, who are not here to answer your questions.

    I would guess that the reason is that it is pointless. The value can only be one of the declared values of that enumeration, so allowing a generalized syntax only provides an opportunity for programmer error. But that's only my opinion.
  • 10. Re: Enum in switch case
    860521 Newbie
    Currently Being Moderated
    Thank you very much for the suggestions jverd.

    I was curious to know the intricacies and delicacies of the language and trying my hand at it.

    I love this language because I am of the opinion that every thing+ in this language has a reason for it to be the way it is and I am chasing that reason wherever I think is possible.

    True that I can spend this time in writing some code as you pointed out, but I am learning how to do it.

    Thanks EJP.

    From what I understood of this discussion, I think it is right to conclude that it is the design of the java compiler and only enum identifiers have to be used as the case labels.
  • 11. Re: Enum in switch case
    Kayaman Guru
    Currently Being Moderated
    sharath wrote:
    I love this language because I am of the opinion that every thing+ in this language has a reason for it to be the way it is and I am chasing that reason wherever I think is possible.
    Well, get ready to be disappointed, because it's not true. Java is in no way perfect and in some cases the design..."mishaps" can't be fixed later on, because of backwards compatibility reason.
    True that I can spend this time in writing some code as you pointed out, but I am learning how to do it.
    But you don't need to learn all those intricacies. It's like trying to learn all the words in a language before trying to speak it. A waste of time.
  • 12. Re: Enum in switch case
    EJP Guru
    Currently Being Moderated
    I am of the opinion that every thing+ in this language has a reason for it to be the way it is
    No doubt.
    and I am chasing that reason wherever I think is possible.
    Sure. But this is a user-to-user forum. Jim Gosling and his successors have never been seen here, at least not by me, and I've been here since late 1997.
    True that I can spend this time in writing some code as you pointed out, but I am learning how to do it.
    Which you can best do by writing code, actually, rather than speculating about metaphysical questions such as 'why'.
    Thanks EJP.
    My pleasure.
    I think it is right to conclude that it is the design of the java compiler
    Well of course it is, but that's just a tautology, isn't it? It doesn't explain anything. My suggestion does explain it, but it's only a suggestion.
  • 13. Re: Enum in switch case
    YoungWinston Expert
    Currently Being Moderated
    Kayaman wrote:
    But you don't need to learn all those intricacies. It's like trying to learn all the words in a language before trying to speak it. A waste of time.
    Nicely put. I'll have to remember that one :-)

    Winston
  • 14. Re: Enum in switch case
    796440 Guru
    Currently Being Moderated
    javaLearner wrote:
    I love this language because I am of the opinion that every thing+ in this language has a reason for it to be the way it is
    That is unlikely. There are almost certainly some decisions that were arbitrary.
    and I am chasing that reason wherever I think is possible.
    Why? How will knowing pointless bits of trivia like this help you?
    True that I can spend this time in writing some code as you pointed out, but I am learning how to do it.
    Except that knowing the answer to this question cannot and will not help you learn how to write code.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points