在枚举中使用最终常量

Use final constant inside an enum

我想知道在枚举中使用 final 变量的最佳方式是什么?我试过了,但出现以下错误:非法前向引用。

enum KeyTypes {

    BOLSA(NONE), // Illegal forward reference

    LLAVE(NONE), // Illegal forward reference

    MAGIC("net.labs.key.magical");

    private static final String NONE = "";

    private final String keyClass;

    KeyTypes(String keyClass).....

}

我建议:

enum KeyTypes {

    BOLSA(), // default constructor

    LLAVE(), // default constructor

    MAGIC("net.labs.key.magical");

    private static final String NONE = "";

    private final String keyClass;

    KeyTypes() {
        this(NONE);
    } 

    KeyTypes(String keyClass).....

} 

使用引用而不是直接 属性 名称分配 属性:

public enum KeyTypes {

    BOLSA(KeyTypes.NONE),   // assign value with reference here
    LLAVE(KeyTypes.NONE), 
    MAGIC("net.labs.key.magical");

    private static final String NONE = "";
    private final String keyClass;

    KeyTypes(String keyClass){
        this.keyClass=keyClass;
    }

}