eclipse 中的 ENUM 问题
我的 Eclipse IDE 中出现以下错误:
在定义字段之前无法引用字段
我尝试使用枚举变量,并且其某些值具有相同的名称。
public enum Enun {
A(STATIK);
private static int STATIK = 1;
private Enun(final int i) {
}
}
谁能告诉我如何解决这个问题?
谢谢 :)
I have the the following error in my eclipse IDE:
Cannot reference a field before it is defined
I try to use an enum variable and some of its values have the same name.
public enum Enun {
A(STATIK);
private static int STATIK = 1;
private Enun(final int i) {
}
}
Could anyone tell me how I can solve this problem please?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您不能在枚举声明中引用枚举的静态成员。如果您想命名这些数字,那么您应该使 STATIK 成为嵌套静态类的成员:
尽管我会质疑这样做的必要性 - 枚举名称应该告诉您有关这些数字的所有信息,并且您应该'不需要额外的静态声明。
Yeah, you can't reference static members of the enum in the enum declaration. If you want to name these numbers, then you should make the STATIK a member of a nested static class:
Although I would question the need for this - the enum name should tell you all you need to know about those numbers, and you shouldn't need an aditional static declaration.
您不能扩展任何其他内容,因为枚举已经扩展了某些内容(按规范),但您可以使用枚举来实现!试试这个
You can't extend anything else because enum extends something already (by specification) but you CAN implement with an enum! Try this
换个方式尝试一下:
这有一个副作用,现在 STATIK 不再是编译时常量,但几乎没有什么地方很重要(在 switch 语句中使用 - 但在那里你应该使用枚举值)。
Try it the other way around:
This has the side-effect that now STATIK is not a compile-time-constant anymore, but there are little locations this matters (usage in switch statements - but there you should use your enum values).
您不能在构造函数中传递 STATIK,如果您想实现此目的,请使用类似
记住 enum 默认情况下是单例的,因此无需为此 int 使用 static。
You cannot pass STATIK in constructor, If you want to achieve this use something like
Remember enum is a singleton by default, so no need to use static for this int.