1

I found this type of code in a repository during my project work. That repository works well fine... but when i am trying to understand the code through an independent test run... giving error!!!

public enum Fruits {
    static {
        APPLE= new Fruits( "APPLE", 0 );
        BANANA = new Fruits( "BANANA", 1 );
       // and so on.
    }
}

I am not able to understand the meaning of calling a constructor of enum inside enum, that too without declaring a constructor.

2
  • please elaborate, post your errors Commented Nov 10, 2012 at 15:23
  • At least post code that is complete enough to compile. Commented Nov 10, 2012 at 15:26

3 Answers 3

6

My guess is that this is actually the product of decompiling real code. This isn't valid Java source code, but is effectively what the compiler will create for you with an enum of:

public enum Fruits {
    APPLE, BANANA;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Just wondering, there should also be a constructor declaration in the decompiled code.
@MarkoTopolnik: Yes, I'd expect so. It's not really clear where the OP got this code from, or whether there's more.
You took it very correct... the code WAS decompiled. I had decompiled the repository... Thanks...:)
4

As usual @JonSkeets's intuition wins out. Compiling the code he provided:

public enum Fruits {
  APPLE, BANANA;
}

Then decompiling with jad yields:

public final class Fruits extends Enum
{

    public static Fruits[] values()
    {
        return (Fruits[])$VALUES.clone();
    }

    public static Fruits valueOf(String s)
    {
        return (Fruits)Enum.valueOf(Fruits, s);
    }

    private Fruits(String s, int i)
    {
        super(s, i);
    }

    public static final Fruits APPLE;
    public static final Fruits BANANA;
    private static final Fruits $VALUES[];

    static
    {
        APPLE = new Fruits("APPLE", 0);
        BANANA = new Fruits("BANANA", 1);
        $VALUES = (new Fruits[] {
            APPLE, BANANA
        });
    }
}

The complete example illustrates all of the work the compiler is doing for you when you declare an enum. Note that, as @MarkoTopolnik points out, you can't do this yourself, simply because the compiler will not allow it.

Comments

-1
    public enum Fruits {
        APPLE("APPLE", 0 ), BANANA("BANANA", 1 );
        String text;
        int value;
        private Fruits(String text, int value){
             this.text = text;
             this.value = value;
        }
        public String getText(){
             return text;
        }
        public int getValue(){
             return value;
        }
    }

This concept is useful when you wish to add more parameters.

3 Comments

Actually, those are implicit arguments to an implicit enum member constructor. They set the name and ordinal properties.
Yeah both of those properties are available on all enums automatically. It would be a complete waste of time to include them in a constructor.
Agree but this helps understanding the concept of constructor and value retrieval. If OP wants to add more parameters in the constructor, it will be very useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.