Make this private enum static or not?
In a class, I need to define different actions which are passed into a method. Now, all instances can safely share these action, so I thought I could safely make the enum static.
But in almost all examples I've seen, the nested enum is never made static. So, which is the preferred way to define a nested (private) enum? Does it actually make sense to not make开发者_开发知识库 it static?
public class MyClass {
private static enum Action {
READ("read"),
WRITE("write");
final String value;
Action(String value) {
this.value = value;
}
String getValue() {
return value;
}
}
// attributes, methods, constructors and so on
}
The Java Language Specification, section 8.9, states:
Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static.
So you can, but you don't have to, and it'll make no difference. Personally I don't think I'd bother, as it's implicit anyway.
Nested enum types are implicitly static :)
If an enum is a member of a class, it is implicitly static
As enum is inherently static, there is no need and makes no difference when using static-keyword in enum.
精彩评论