5

Are there any objects created respective to each of the enum constants ARROGANT, RASCAL, IDIOT?

public enum Manager {
    ARROGANT,
    RASCAL,
    IDIOT
}

and if the following code does the same as the above, explicitly though,

public enum Manager {
    ARROGANT(),
    RASCAL(),
    IDIOT();

    Manager() {}
}
1
  • Note: you do not need the constructor in the second code. Commented May 31, 2011 at 12:05

2 Answers 2

6

Yes, exactly one instance will be created for each enum constant.

And yes, the second sample code is effectively identical.

Sign up to request clarification or add additional context in comments.

Comments

4

Yes, both should result in the same bytecode, the first is only syntactic sugar.

The second is useful when you have to associate values with an enum.

enum Numbers{
    ONE(1),TWO(2),THREE(3),TEN(10);
    Numbers(int i){
       value = i;
    }
    public final int value;
}

Comments

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.