Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an interface - here's a nicely contrived version as an example:

public interface Particle {      enum Charge {         POSITIVE, NEGATIVE     }      Charge getCharge();      double getMass();      etc... } 

Is there any difference in how implementations of this would behave if I defined the Charge enum as static - i.e. does this have any effect:

public interface Particle {      static enum Charge {         POSITIVE, NEGATIVE     }      Charge getCharge();      double getMass();      etc... } 
like image 460
serg10 Avatar asked Oct 31 '08 11:10

serg10


2 Answers

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} } 
like image 151
idrosid Avatar answered Sep 26 '22 21:09

idrosid


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.

like image 21
Jon Skeet Avatar answered Sep 22 '22 21:09

Jon Skeet