Why use static enumeration?

I tried to find the difference between an enum static declaration?

public class Example { public static enum Days { MONDAY(1); private int day; private Days(int day) { this.day = day; } public int getDayNum() { return day; } } } 

And below

 public class Example { public enum Days { MONDAY(1); private int day; private Days(int day) { this.day = day; } public int getDayNum() { return day; } } } 

I can access both of the above in the same way

 Example.Days.MONDAY.getDayNum(); 

This is because the enumeration is static, final . So what is the difference? When to use any of the above?

+4
source share
4 answers

According to JLS 8.9 :

Nested enumeration types are implicitly static. It is permissible to explicitly declare a nested enumeration type static.

This means that it is not possible to define a local (ยง14.3) enumeration or to define an enumeration in an inner class (ยง8.1.3).

+10
source

Enums implicitly public static final .

Thus, there is no difference when using static-keyword in listings.

+4
source

There is no difference - Java in many cases allows you to use redundant keywords, allowing you to be explicit if you decide so. In general, however, he understood that the enumerations are static, so there is no reason to think of it this way.

+2
source

Docs say

An enumeration type is implicitly finite if it contains at least one enumeration constant that has the class body.

Nested enumeration types are implicitly static . It is allowed to explicitly declare a nested enum type as static.

+1
source

Source: https://habr.com/ru/post/1489122/


All Articles