java enum private_关于java:枚举中的私有静态最终变量

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

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) {

...

}

}

...

}

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

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语言规范的强制要求。但有两个快速的事情:

您正在使用private static final Strings。这给了你

相反,使用字符串文字绝对没有任何好处,

这将解决问题。

如果你想声明可重用的常量(public static final

Strings),那么你需要在枚举之外这样做。

或者,您可以将Enums声明为类的嵌套元素,为您定义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";

}

公约说要把你的方法放在你的方法上。

我实际上是在构造函数中使用字符串。

汤姆 - 你说"把你的方法放在你的方法上"是什么意思?

@will - 我打赌他正在谈论编码风格惯例。通常,您可以将实例变量放在类定义的顶部。在这种情况下,您可以将枚举变量放在枚举文字声明和方法定义之间。另外,如果我的回答对您有帮助,请随时接受!

@will - 自从我回答之后,我看到你编辑了你的问题。请参阅上面我的回答的编辑。

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

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

}

...

}

真棒! (和奇怪的)

我正在获得一个非法的前向参考,但我正在传递已编译的模式。嵌套在枚举中的接口实现了这个技巧。

似乎编译(奇怪)但在运行时我得到java.lang.NoClassDefFoundError: Could not initialize class

编译但提供运行时异常

这不是有效的Java代码。

可以使用接口:

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) {

...

}

}

...

}

如果您不希望在另一个类中包含这两个接口,则可以将接口嵌套在枚举中。

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

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

}

}

对前面答案的反对意见是1)有些不起作用,2)有些没有真正的常量,3)有些需要嵌入式或额外的类。这样做怎么样?

public class Constants {

public static final double EQUATORIALRADIUS = 6378137.0;

}

public enum Planet {

EARTH (Constants.EQUATORIALRADIUS),

"但是!",你说,"还有一个额外的课程!"

不,你只需要在编译之后和dexing之前删除Constants.class文件或者压缩到jar。

或者你可以Proguard它。在任何情况下,您都可以使用apktool验证Constants类的任何位置都没有引用。所有常量都是真正的常量,并与smali内联。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值