0

I am having some small doubts regarding Enums. I have the following confusions :-

  1. What is the use of static methods in Enums? Mostly we use Enums for declaring constants. What is the use of this feature?
  2. I see, an Enum can implement an interface. What would be the use? I am just confused about practical life usage.
  3. Another confusion is, see the code snippet below,

    enum Position {
        FIRST, SECOND, THIRD;
        public static void main(String[] args) { //line 1
            Position p = Position.SECOND; //line 2
            switch(Position.THIRD) { //line 3
                case FIRST: //line 4
                    System.out.println("Congrats, You are the winner");//line 5
                    break; //line 6
                case SECOND : //line 7
                    System.out.println("Congrats, You are the runner");//line 8
                    break; //line 9
                default : //line 10
                    System.out.println("Better luck next time"); //line 11
            }
        }
    }
    

    If use in case statement like Position. First,it gives a compile time error. I understand JLS does not allow this. Reason written is with a class format, it can't make a symbolic linkage. But reverse is true. Like in line-3, i am using the enum itself with fully qualified name. So, my point is, what is the meaning of symbolic linkage and why line-3 is allowed also?

Note It was basically a typo mistake. My main question was, we are using syntax Position.THIRD in switch condition(which is allowed by compiler) but when we use same syntax in case, it is not allowed. Why is that?

Quote From JLS

(One reason for requiring inlining of constants is that switch statements require constants on each case, and no two such constant values may be the same. The compiler checks for duplicate constant values in a switch statement at compile time; the class file format does not do symbolic linkage of case values.)

Here it mentions about Symbolic linkage. But when i wrote something like this in switch condition switch(Position.THIRD), it was allowed where as in CASE statement it was.

benz
  • 4,561
  • 7
  • 37
  • 68
  • 1
    If you're going to give code that doesn't compile and say there's an error, *please* give the details of the error - the exact message, with the line causing the problem. The problem with `switch (Position.Third)` is just that it should be `Position.THIRD`. Java is case-sensitive. – Jon Skeet Jan 16 '15 at 10:23
  • @JonSkeet, it was a typo mistake, i have updated my question and made a note of what i was trying to ask basically. – benz Jan 16 '15 at 10:29
  • ... and in the process, made answers nonsensical. Please try to avoid doing that in future. – Jon Skeet Jan 16 '15 at 10:33
  • @JonSkeet, i do apologize, i have updated it now. – benz Jan 16 '15 at 10:36
  • But no link and no section number, so we can't tell which section you're talking about. And that paragraph really isn't talking about why you can't write `Position.FIRST` instead of `Position`. – Jon Skeet Jan 16 '15 at 10:37

3 Answers3

2

What is the use of static methods in Enums?

Same as in any other type. For enums, they're often "given this string value, return the equivalent enum" or something similar. (In cases where valueOf isn't appropriate, e.g. where there are multiple representations.)

I see, an Enum can implement an interface. What would be the use?

Same as with any interface - to decouple implementation from usage. For example, I've made enums implement a localization-related interface before now.

My main question was, we are using syntax Position.THIRD in switch condition(which is allowed by compiler) but when we use same syntax in case, it is not allowed. Why is that?

It's just a language decision - the enum type has to be the same as the type of the switch value itself, so it's redundant information. It has nothing to do with the section of the JLS you quoted.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • can you help me with point-3 and what is the meaning of symbolic linkage? Jon, it was a written mistake. My basic question, i will update it now. – benz Jan 16 '15 at 10:24
  • @benz: Added an explanation - basically you had a typo. If you'd specified the exact error message, it would have been easier to help you. But when the compiler says it can't find a symbol, you should really check whether that symbol exists... – Jon Skeet Jan 16 '15 at 10:26
  • Jon, i updated my question, can you please help me with that. – benz Jan 16 '15 at 10:29
  • @benz: It's hard to answer it when you haven't given enough context. If you're quoting from the JLS, please link to the *exact* part you're quoting from. – Jon Skeet Jan 16 '15 at 10:32
0

Enums can make pretty nice Singletons ... that basically answers all of your questions :)

light_303
  • 2,101
  • 2
  • 18
  • 35
0

Enums are a lot like normal classes, except they have certain restrictions on instantiation and they can't extend classes, etc.

Static methods are typically factory methods that return instances of the enum, often looking up the instance whose field matches a parameter. See this example.

Enums can have fields, getters and setters and other methods and can thus implement interfaces - including generic ones. See this example.

You use an enum instead of a class when you want a finite set if named instances.

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722