I was trying to find the difference between declaring an enum static?
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 the one 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 the exact same way
Example.Days.MONDAY.getDayNum();
This is because an enum is static, final. So whats the difference? When to use either of the above?
As per the JLS 8.9:
Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static.
This implies that it is impossible to define a local (§14.3) enum, or to define an enum in an inner class (§8.1.3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With