-1

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?

  • 4
    They are `static` so that the constants are associated with the class instead of an instance of the class. This ensures the constants are singletons, and that you can reference them without first needing an instance of the enum (which would not be possible to obtain as you are not allowed to instantiate enums yourself). They are `public` so that they are visible to outside code. Not sure what else to say about that, other than you control the visibility of an enum by changing the visibility of the enum class itself. โ€“ Slaw Jul 30 '22 at 09:40
  • 3
    As a counter-question: why would you think it makes sense for them not to be `static` or `public`? โ€“ ndc85430 Jul 30 '22 at 09:45

2 Answers2

3

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.

2

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 modifier abstract, final, sealed, or non-sealed."

    See below.

  • "A NESTED enum class is implicitly static. That is, every member enum class and local enum class is static."

    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, and private (ยง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 implicitly final OR implicitly sealed ..."

    There is something rather subtle going on here. If an enum constant has a class body, then it actually defines an anonymous subclass of the enum class. In this case, the enum class is not final in the sense of "having no subclasses"


So:

  1. Enum classes are NOT implicitly public. They can be private, for example.

  2. Enum classes MAY BE implicitly final in the "has no subclasses" sense. But you were using final in the "constantness" sense. The binding between an enum 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 that 42 is a constant. Just like you can change the array content with the following "constant":

     final int[] CONST = new int[]{1, 2, 3};
    
  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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216