2012-02-14 42 views
17

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

Multiple markers at this line

  • Syntax error, insert "Identifier" to complete EnumConstantHeaderName
  • Syntax error, insert "}" to complete 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) { 
     ... 
    } 
    } 
... 
} 

回答

20

枚举常数需要为在枚举的第一要素。你的代码的版本,编译:

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

*编辑*

到你原来的问题作出

基于关闭的编辑,我看到你正在尝试做的。不可以在枚举定义本身中声明的枚举文字声明中使用常量。这是因为字面声明必须是枚举中的第一个元素。这是由Java语言规范强制的。两件很快的事情:

  1. 您正在使用private static final Strings。这使你 完全没有任何好处,而不是使用字符串文字, 这将解决问题。
  2. 如果您想声明可重复使用的常量(public static final Strings),那么您需要在枚举的之外执行

或者你可以声明您枚举为它定义了private static final String是给你的一类嵌套元素。

一些伪代码:

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"; 
} 
+0

公约说,把你的领域关于你的方法。 – 2012-02-14 01:21:24

+1

我实际上是在构造函数中使用字符串。 – will 2012-02-14 01:51:03

+0

汤姆 - 你说“把你的领域放在你的方法上”是什么意思? – will 2012-02-14 01:51:29

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) { 
     ... 
    } 
    } 
... 
} 
+1

如果你不想在另一个类中包含这两个接口,你可以在这个枚举中嵌套这个接口。 – MithrilTuxedo 2016-08-17 19:57:58

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"; 
    } 
... 
} 
+0

太棒了! (和奇怪) – TWiStErRob 2016-07-18 23:15:09

+1

我收到了一个非法的前向引用,但我传递了编译模式。尽管嵌套在枚举中的接口做到了这一点。 – MithrilTuxedo 2016-08-17 19:57:05

+0

似乎编译(奇怪),但然后在运行时我得到'java.lang.NoClassDefFoundError:无法初始化类' – qwertzguy 2016-08-25 20:45:12