java - Static enum vs. Non-static enum - Stack Overflow
All enum
s are effectively static
. If you have a nested enum, it is much the same as a 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.
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
interface method - public abstract
nested class in an interface - public static
, non-final
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)
EDIT: The only place you need to use static
with enum
is with import static
of an enum's value. Thank you @man910
Read full article from java - Static enum vs. Non-static enum - Stack Overflow
No comments:
Post a Comment