I have been recently studying java enums and couldn't understand why they are implicitly public and static by nature. final I understand because they are constants but why the other tags?
CodePudding user response:
If you would look at Java documentation, it is clearly mentioned that:
Programmers can not invoke constructors of enum.
which basically means that we can not create any object of enum using the keyword new
. Now if enums weren't static, then how would we access them without any instance/object?
enum Color { RED, GREEN, BLUE; } // enum declaration
Color.RED //accessing enum constant
If you have noticed only have to access enum constants is through enum name (similar to how we access static members of any class). So to be able to access enum constants without any object we need them to be static.
And enums are by default public so that we can freely access them anywhere however this is not a necessity we can use private or protected modifiers as well.
CodePudding user response:
I have been recently studying Java
enums
and couldn't understand why they are implicitly public and static by nature.
final
I understand because they are constants but why the other tags?
It is complicated, but I think you have some of the facts incorrect there.
According to the Java 17 Language Specification (JLS 8.9)
"It is a compile-time error if an
enum
declaration has the modifierabstract
,final
,sealed
, ornon-sealed
."See below.
"A NESTED
enum
class is implicitlystatic
. That is, every memberenum
class and localenum
class isstatic
."And non-nested classes are implicitly static too.
"It is a compile-time error if ... an enum declaration has MORE THAN ONE OF the access modifiers
public
,protected
, andprivate
(§6.6)."But that is the same as any other class. This doesn't say that it is implicitly
public
."An
enum
class is either implicitlyfinal
OR implicitlysealed
..."There is something rather subtle going on here. If an
enum
constant has a class body, then it actually defines an anonymous subclass of theenum
class. In this case, theenum
class is not final in the sense of "having no subclasses"
So:
Enum classes are NOT implicitly
public
. They can beprivate
, for example.Enum classes MAY BE implicitly
final
in the "has no subclasses" sense. But you were usingfinal
in the "constantness" sense. The binding between anenum
constant name and the corresponding value cannot change; i.e. it is implicitly final in that sense.However, the
enum
values can have mutable fields, so they are not necessarily constant in the sense that42
is a constant. Just like you can change the array content with the following "constant":final int[] CONST = new int[]{1, 2, 3};
Enum classes ARE implicitly
static
in contexts where another class could be non-static.
Why are they implicitly static? Well if they weren't, what would it mean? An implicitly static enum
is effectively a set of singleton values. But it it wasn't, then each time you created an instance of the class that enclosed the enum
class, you would be creating a new set of enum
values. They are no longer singleton. This would be most unexpected ... and I am finding it hard to see how it would be useful.