What's is the difference between a static and non-static annotation?

Brandon Yarbrough :

Java's inner classes can be static or non-static. Non-static inner classes are tied to an instance of the enclosing class.

Annotations are a type of Java interface, and like any other class, they can be defined inside a class. Similarly, they can be declared static or non-static. What is the difference between these two choices, is there any difference between how they're used by consuming code, and is there any scenario where it'd make sense to use one or the other?

Example:

public class AnnotationContainer {

  public static @interface StaticAnnotation {}
  public @interface NonstaticAnnotation {}

}
Andy Turner :

No difference at all. Nested interfaces are always static.

This is described in JLS Sec 8.5.1 (for classes):

A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.

and JLS Sec 9.5 (for interfaces):

A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=467012&siteId=1