java java枚举变量是静态的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17166668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are java enum variables static?
提问by Optional
public enum Operations {
SINGLE,
MULTIPLE;
private Type operation;
public void setOperation(Type operation) {
this.operation = operation;
}
public Type getOperation() {
return operation;
}
public static void main(String[] args) {
Operations oper1 = Operations.SINGLE;
oper1.setOperation(Type.GET);
Operations oper2 = Operations.SINGLE;
oper2.setOperation(Type.POST);
System.out.println(oper1.getOperation());
System.out.println(oper2.getOperation());
}
}
enum Type {
POST,
GET;
}
In the above code, the value of operation changes for both the Operations. How can I have two instances of Operations.SINGLE with different operation type?
在上面的代码中,两个操作的操作值都发生了变化。我怎样才能有两个不同操作类型的 Operations.SINGLE 实例?
回答by Eric Jablow
Yes, instances are implicitly static
and final
. This means that the code is unwise. Imagine two threads both calling SINGLE.setOperation(Type)
; you will have no confidence in what you are calling.
是的,实例是隐式的static
和final
。这意味着代码是不明智的。想象两个线程都调用SINGLE.setOperation(Type)
; 你对自己的呼唤没有信心。
From the Java Language Specification, Section 8.9:
Enum types (§8.9) must not be declared abstract; doing so will result in a compile-time error.
An enum type is implicitly final unless it contains at least one enum constant that has a class body.
It is a compile-time error to explicitly declare an enum type to be final.
Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static.
枚举类型(第 8.9 节)不得声明为抽象类型;这样做会导致编译时错误。
枚举类型是隐式 final 的,除非它包含至少一个具有类主体的枚举常量。
将枚举类型显式声明为 final 是编译时错误。
嵌套的枚举类型是隐式静态的。允许将嵌套枚举类型显式声明为静态。
And in the next section:
在下一节中:
The body of an enum type may contain enum constants. An enum constant defines an instance of the enum type.
Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
枚举类型的主体可能包含枚举常量。枚举常量定义了枚举类型的实例。
因为每个枚举常量只有一个实例,所以在比较两个对象引用时,如果已知其中至少一个引用一个枚举常量,则允许使用 == 运算符代替 equals 方法。
回答by dasblinkenlight
How can I have two instances of Operations.SINGLE with different operation type?
我怎样才能有两个不同操作类型的 Operations.SINGLE 实例?
The fundamental idea behind an enum
is that there is one, and only one, instance of each of its members. This is what lets you safely compare them for equality, without fear that there's another SINGLE
or MULTIPLE
created in some other place.
an 背后的基本思想enum
是它的每个成员只有一个实例。这使您可以安全地比较它们是否相等,而不必担心存在另一个SINGLE
或MULTIPLE
在其他地方创建。
If you want multiple instances of SINGLE
, make it a class
, not an enum
. The fact that you made your enum
mutable indirectly points in the same direction: using enum
is a wrong choice in your situation.
如果您想要 的多个实例SINGLE
,请将其设为class
,而不是enum
。您enum
间接地使可变变量指向同一方向这一事实:enum
在您的情况下使用是错误的选择。
回答by Bohemian
Enum instances are "static" (ie behave like static variables), but are not immutable.
枚举实例是“静态的”(即表现得像静态变量),但不是不可变的。
All threads see the same object referred to by the enum name - they are like singletons, with an iron-clad guarantee from the JVM that there is only ever one instance of an enum. Changing a field of an enum changes it for everyone.
所有线程都看到由 enum 名称引用的同一个对象——它们就像单例一样,从 JVM 那里得到铁一般的保证,即只有一个 enum 实例。更改枚举的字段会为每个人更改它。
It is good practice to make your fields final
in an enum and to make them immutable.
final
在枚举中创建字段并使其不可变是一种很好的做法。
回答by Vlasec
I'm one year and a half too late. But I see the question was not really answered.
我迟到了一年半。但我看到这个问题并没有得到真正的回答。
The solution would be using a class instead of enum, that has those two enums as its fields:
解决方案是使用类而不是枚举,它将这两个枚举作为其字段:
class Operation {
Quantity quantity;
Type type;
Operation(Quantity quantity, Type type) {
this.quantity = quantity;
this.type = type;
}
}
You can, of course, use enum instead of class. Then you'll have to enumerate all combinations:
当然,您可以使用枚举而不是类。然后你必须枚举所有组合:
enum Operation {
SINGLE_GET(Quantity.SINGLE, Type.GET)
SINGLE_POST(Quantity.SINGLE, Type.POST)
MULTIPLE_GET(Quantity.MULTIPLE, Type.GET)
// ... more options
// contents same as in class Operation, constructor private by default
}
Both approaches are valid, sometimes you really want to enumerate all the combinations, most of the time, however, you should probably stick with the class
approach.
这两种方法都是有效的,有时您确实想枚举所有组合,但大多数情况下,您可能应该坚持使用该class
方法。
For brevity, I didn't define the enums Quantity
and Type
, they are just simple enums.
为简洁起见,我没有定义枚举Quantity
和Type
,它们只是简单的枚举。
回答by Mubin
Yes, all elements of enum
are static final constant
.
However as mentioned in another answer by darijan
, there is a logical mistake in your program.
是的, 的所有元素enum
都是static final constant
。但是,正如在另一个答案中提到的darijan
,您的程序存在逻辑错误。
回答by darijan
There is an error in fourth line of main
method
第四行main
方法有错误
oper1.setOperation(Type.POST);
should be
应该
oper2.setOperation(Type.POST);