Java 静态枚举与非静态枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23127926/
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
Static enum vs. Non-static enum
提问by AHHP
What's the difference between static and non-static enum in Java? Both usages are same.
Java 中的静态枚举和非静态枚举有什么区别?两者用法相同。
Is it correct that all static ones are loaded on memory on startup, and non-static ones are loaded on demand?If yes, then which method is better? Keeping some data always in memory or using server resources to load them each time?
是否正确 所有静态的都在启动时加载到内存中,非静态的按需加载?如果是,那么哪种方法更好?将某些数据始终保留在内存中还是每次都使用服务器资源来加载它们?
public class Test {
public enum Enum1 {
A, B
}
public static enum Enum2 {
C, D
}
public static void main(String[] args) {
Enum1 a = Enum1.A;
Enum1 b = Enum1.B;
Enum2 c = Enum2.C;
Enum2 d = Enum2.D;
}
}
采纳答案by Peter Lawrey
All enum
s are effectively static
. If you have a nested enum, it is much the same as a static class
.
所有enum
s 都是有效的static
。如果您有嵌套的枚举,则它与static class
.
All classes are lazily loaded (enums or otherwise) however when they are loaded they are loaded all at once. i.e. you can't have a few constants loaded but not others (except in the middle of class initialization)
所有类都被延迟加载(枚举或其他),但是当它们被加载时,它们会被一次性加载。即你不能加载一些常量但不能加载其他常量(除了在类初始化过程中)
Java allows certain modifiers to be implicit to avoid having to declare them all the time. This means that adding a modifier doesn't necessarily do anything other than provide a longer way of writing the same thing.
Java 允许某些修饰符是隐式的,以避免一直声明它们。这意味着添加修饰符除了提供更长的编写相同内容的方法外,不一定会做任何事情。
Default modifiers for
默认修饰符
class field/method/nested class - package local, non-final, non-static
类字段/方法/嵌套类 - 包本地、非最终、非静态
enum and nested enum - package local, final and static
枚举和嵌套枚举 - 包本地、最终和静态
interface field - public static final
接口字段 - public static final
interface method - public abstract
接口方法—— public abstract
nested class in an interface - public static
, non-final
接口中的嵌套类 - public static
,非最终的
Note: while static
is optional for an enum
it is always static. However, final
cannot be set for an enum even though it is always notionally final
(Technically you can have subclasses with overridden implementations for constants)
注意:whilestatic
是可选的,enum
它始终是静态的。但是,final
不能为枚举设置,即使它始终是概念上的final
(从技术上讲,您可以拥有具有重写的常量实现的子类)
EDIT: The only place you need to use static
with enum
is with import static
of an enum's value. Thank you @man910
编辑:您需要使用的唯一的地方static
有enum
是import static
一个枚举的价值。谢谢@man910
回答by Konstantin Yovkov
If you're talking about nested enums, they are implicitly static
by default.
如果您在谈论嵌套枚举,则static
默认情况下它们是隐式的。
According to the Java Language Specification:
根据Java 语言规范:
Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static.
嵌套的枚举类型是隐式静态的。允许将嵌套枚举类型显式声明为静态。
回答by Elliott Hill
All Enums are implicitly static, its just you don't need to write the static
keyword. Similarly to the way all interface methods are implicitly public.
所有枚举都是隐式静态的,只是您不需要编写static
关键字。与所有接口方法都隐式公开的方式类似。
回答by user3145373 ツ
As enums
are inherently static
, there is no need and makes no difference when using static-keyword
in enums
.
照enums
原样inherently static
,在使用static-keyword
in时没有必要也没有区别enums
。
If an enum is a member of a class, it is implicitly static.
如果枚举是类的成员,则它是隐式静态的。
Interfaces may contain member type declarations. A member type declaration in an interface is implicitly static and public.
接口可能包含成员类型声明。接口中的成员类型声明是隐式静态和公共的。