简体   繁体   English

java 枚举类型实例化

[英]java enum type instantiation

Are there any objects created respective to each of the enum constants ARROGANT, RASCAL, IDIOT?是否有针对每个枚举常量 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() {}
}

Yes, exactly one instance will be created for each enum constant.是的,将为每个枚举常量创建一个实例。

And yes, the second sample code is effectively identical.是的,第二个示例代码实际上是相同的。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM