55

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...
}
0

2 Answers 2

88

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}
}
1
  • 3
    I'd argue that it's both - it's implicitly static for two different reasons, but either of them would have been enough on its own.
    – Jon Skeet
    Oct 31, 2008 at 12:24
31

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.

1
  • yes (+1) and actually checkstyle would flag the 'static' as being redundant and would ask you to remove it. Variables in interfaces and annotations are automatically public, static and final, so these modifiers are redundant as well.
    – VonC
    Oct 31, 2008 at 11:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.