Nested Java enum definition - does declaring as static make a difference? [duplicate]

吃可爱长大的小学妹 提交于 2019-11-27 18:27:38

No, it makes no difference. However the reason is not because it is a member declaration inside an interface, as Jon says. The real reason is according to language spec (8.9) that

Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static.

At the following example static does not make any difference either (even though we have no interface):

public class A {
  enum E {A,B};
}

public class A {
  static enum E {A,B};
}

Another example with a nested private enum (not implicitly public).

public class A {
  private static enum E {A,B}
}
Jon Skeet

No, it makes no difference. From the language spec, section 9.5:

Interfaces may contain member type declarations (§8.5). A member type declaration in an interface is implicitly static and public.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!