是否必须仅将注释(例如@SampleAnnotation)附加到类声明(放置在该类声明之前)?我们可以在要应用@SampleAnnotation标记的方法之前使用它吗?

最佳答案

这取决于注释。这些是注释的可能位置(来自javadoc @Target

> ANNOTATION_TYPE Annotation type declaration
> CONSTRUCTOR Constructor declaration
> FIELD Field declaration (includes enum constants)
> LOCAL_VARIABLE Local variable declaration METHOD Method declaration
> PACKAGE Package declaration
> PARAMETER Parameter declaration
> TYPE Class, interface (including annotation type), or enum declaration

10-07 12:01