静态和非静态注释有什么区别?
What's is the difference between a static and non-static annotation?
Java 的内部 classes 可以是静态的也可以是非静态的。非静态内部 classes 绑定到封闭 class.
的实例
注解是一种 Java 接口,与任何其他 class 一样,它们可以在 class 中定义。同样,它们可以声明为静态或非静态。这两种选择之间有什么区别,它们在使用代码时的使用方式有什么区别,是否存在使用其中一种有意义的场景?
示例:
public class AnnotationContainer {
public static @interface StaticAnnotation {}
public @interface NonstaticAnnotation {}
}
完全没有区别。嵌套接口始终是静态的。
这在 JLS Sec 8.5.1 中有描述(对于 类):
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.
和JLS Sec 9.5(对于接口):
A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.
进一步说明 它们完全相同,因为它们是一种特殊的接口声明,并且 "member interfaces" 无论如何都是隐式静态的:
An annotation type declaration specifies a new annotation type, a special kind of interface type. To distinguish an annotation type declaration from a normal interface declaration, the keyword interface is preceded by an at-sign (@).
JLS 10 8.5.1. Static Member Type Declarations :
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.
和JLS 10 9.1.1. Interface Modifiers
The modifier static pertains only to member interfaces (§8.5.1, §9.5), not to top level interfaces (§7.6).
旁注:有趣的是,这些章节没有使用第 9 章顶部定义的术语 "nested interface",但它似乎是 "member interface" 的同义词:
A nested interface is any interface whose declaration occurs within the body of another class or interface.
Java 的内部 classes 可以是静态的也可以是非静态的。非静态内部 classes 绑定到封闭 class.
的实例注解是一种 Java 接口,与任何其他 class 一样,它们可以在 class 中定义。同样,它们可以声明为静态或非静态。这两种选择之间有什么区别,它们在使用代码时的使用方式有什么区别,是否存在使用其中一种有意义的场景?
示例:
public class AnnotationContainer {
public static @interface StaticAnnotation {}
public @interface NonstaticAnnotation {}
}
完全没有区别。嵌套接口始终是静态的。
这在 JLS Sec 8.5.1 中有描述(对于 类):
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.
和JLS Sec 9.5(对于接口):
A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.
进一步说明
An annotation type declaration specifies a new annotation type, a special kind of interface type. To distinguish an annotation type declaration from a normal interface declaration, the keyword interface is preceded by an at-sign (@).
JLS 10 8.5.1. Static Member Type Declarations :
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.
和JLS 10 9.1.1. Interface Modifiers
The modifier static pertains only to member interfaces (§8.5.1, §9.5), not to top level interfaces (§7.6).
旁注:有趣的是,这些章节没有使用第 9 章顶部定义的术语 "nested interface",但它似乎是 "member interface" 的同义词:
A nested interface is any interface whose declaration occurs within the body of another class or interface.