java - 静态和非静态注释有什么区别?

标签 java java-annotations

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/

相关文章:

java - 为什么 JPMS 允许注解类型作为服务

java - spring boot中外键引用主键

Android Studio 警告 - InnerClass 注释缺少相应的 EnclosureMember 注释

java - 无法在面向对象编程、数组中为函数赋值

java - 如何在各个 JUnit 测试用例之间实现公共(public)数据共享

java - mDrawerList.setOnItemClickListener(new SlideMenuClickListener()) 上的 NullPointerException

java - Java 程序的程序计数器寄存器值

java - Java注释@Suppresswarnings如何工作?

Java注释: using a field value into another field

java - 当您单独调用 getChildren 时会发生什么?