Java 的内部类可以是静态的也可以是非静态的。非静态内部类绑定(bind)到封闭类的实例。
注解是一种 Java 接口(interface),与任何其他类一样,它们可以在类中定义。同样,它们可以声明为静态或非静态。这两种选择之间有什么区别,它们在使用代码时的使用方式有什么区别,是否存在使用其中一种选择有意义的场景?
例子:
public class AnnotationContainer {
public static @interface StaticAnnotation {}
public @interface NonstaticAnnotation {}
}
最佳答案
完全没有区别。嵌套接口(interface)始终是静态的。
这在 JLS Sec 8.5.1 中有描述(对于类(class)):
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 (对于接口(interface)):
A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.
关于java - 静态和非静态注释有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50843629/