枚举中的私有静态最终变量

wil*_*ill 22 java enums compiler-errors

我正在尝试在枚举中创建一个私有静态最终变量,但我不断收到编译错误.有谁知道如何解决这一问题?

此行有多个标记

  • 语法错误,插入"Identifier"以完成EnumConstantHeaderName
  • 语法错误,插入"}"以完成EnumBody
class Foo {
  ...

  public enum MyEnum {
    private static final String MY_STRING = "a string I use in a constructor";
    private static final String MY_OTHER_STRING = "a string I use in another constructor";      

    MyEnumType(1, MY_STRING),
    MyEnumType2(2, MY_STRING),
    MyEnumType3(3, MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    } 
  }
 ...
}
Run Code Online (Sandbox Code Playgroud)

Per*_*ion 24

枚举常量需要是枚举中的第一个元素.您编译的代码版本:

class Foo {

    public enum MyEnum {
        MyEnumType, MyEnumType2;

        public String bar() {
            return MY_STRING;
        }

        public String bar2() {
            return MY_STRING + "2";
        }

        private static final String MY_STRING = "a string I reuse in the enum";
    }
}
Run Code Online (Sandbox Code Playgroud)

*编辑*

根据您对原始问题的编辑,我看到您要做的事情.不可能在枚举定义本身声明的枚举文字声明中使用常量.这是因为文字声明必须是枚举中的第一个元素.这是Java语言规范的强制要求.但有两个快速的事情:

  1. 你正在使用private static final Strings.这对于使用字符串文字而言绝对没有任何好处,这将解决问题.
  2. 如果你想声明可重用的常量(public static final Strings),那么你需要在枚举之外这样做.

或者,您可以将Enums声明为类的嵌套元素,private static final String为您定义's'.

一些伪代码:

public class Foo {
    public static enum MyEnum {
        MyEnumType(0, MY_STRING), MyEnumType2(1, "Hello");

        private int ordinal;
        private String value;

        MyEnum(int ordinal, String value) {
            this.ordinal = ordinal;
            this.value = value;
        }

        public int getOrdinal() {
            return ordinal;
        }

        public String getValue() {
            return value;
        }

        public void setOrdinal(int ordinal) {
            this.ordinal = ordinal;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    private static final String MY_STRING = "a string I reuse in the enum";
}
Run Code Online (Sandbox Code Playgroud)


Jug*_*aut 19

有可能,您只需要直接引用变量.

class Foo {
  ...

  public enum MyEnum {

    MyEnumType(1, MyEnum.MY_STRING),
    MyEnumType2(2, MyEnum.MY_STRING),
    MyEnumType3(3, MyEnum.MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    }

     private static final String MY_STRING = "a string I use in a constructor";
     private static final String MY_OTHER_STRING = "a string I use in another constructor";  
  }
 ...
}
Run Code Online (Sandbox Code Playgroud)


tig*_*tig 6

可以使用接口:

class Foo {
  ...

  private interface MyEnumConstants {
    static final String MY_STRING = "a string I use in a constructor";
    static final String MY_OTHER_STRING = "a string I use in another constructor";      
  }

  public enum MyEnum implements MyEnumConstants {
    MyEnumType(1, MY_STRING),
    MyEnumType2(2, MY_STRING),
    MyEnumType3(3, MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    } 
  }
 ...
}
Run Code Online (Sandbox Code Playgroud)