Forum Stats

  • 3,839,341 Users
  • 2,262,485 Discussions
  • 7,900,938 Comments

Discussions

Enum in switch case

javaLearner
javaLearner Member Posts: 3
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

Answers

  • darrylburke
    darrylburke Member Posts: 18,007
    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
  • YoungWinston
    YoungWinston Member Posts: 4,310
    edited Aug 15, 2011 8:46AM
    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
  • 875329
    875329 Member Posts: 38
    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.
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    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.
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    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
  • YoungWinston
    YoungWinston Member Posts: 4,310
    edited Aug 15, 2011 1:50PM
    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
  • javaLearner
    javaLearner Member Posts: 3
    edited Aug 16, 2011 4:10AM
    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
  • 796440
    796440 Member Posts: 19,179 Gold Trophy
    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.
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    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.
  • 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.
This discussion has been closed.