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

时间:2012-02-14 00:56:50

标签: 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) {
      ...
    } 
  }
 ...
}

5 个答案:

答案 0 :(得分: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";
    }
}

*编辑*

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

  1. 您正在使用private static final Strings。这给了你 相反,使用字符串文字绝对没有任何好处, 这将解决问题。
  2. 如果您想申报可重复使用的常量(public static final Strings),那么您需要这样做 枚举。
  3. 或者,您可以将您的枚举声明为类的嵌套元素,为您定义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";
    }
    

答案 1 :(得分: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";  
  }
 ...
}

答案 2 :(得分: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) {
      ...
    } 
  }
 ...
}

答案 3 :(得分:1)

您可以像这样在枚举中声明接口

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

    MyEnum(int num, String str) {

    }

    interface EnumConstants {
        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";
    }
}

答案 4 :(得分:0)

对先前答案的异议是:1)有些不起作用,2)有些没有真正的常量,3)有些需要嵌入的或额外的类。这样怎么办?

public class Constants {
    public static final double EQUATORIALRADIUS = 6378137.0;
}

public enum Planet {
    EARTH (Constants.EQUATORIALRADIUS),

“但是!”,您说,“还有一堂课!” 不会,您只需在编译后,解压缩或压缩到jar之前删除Constants.class文件。 或者,您可以保护它。无论如何,您都可以使用apktool验证Constants类的任何地方都没有引用。所有常量都是真正的常量,并且与smali内联。

相关问题