简体   繁体   中英

Which are the default modifiers for fields and methods in a Java annotation?

Which are the default modifiers for x and m in

public @interface Anno {
    int m() default x;
    int x = 10;
}

?

I suppose that the code above is equivalent to:

public @interface Anno {
    public int m() default x;
    public static final int x = 10;
}

where the modifiers public and public static final are redundant, but I didn't find an official explanation for this.

I was looking here: https://docs.oracle.com/javase/8/docs/technotes/guides/language/annotations.html https://docs.oracle.com/javase/tutorial/java/annotations/index.html http://www.vogella.com/tutorials/JavaAnnotations/article.html

Is there any documentation regarding those modifiers? Or could someone provide a "formal" explanation?

Yes, I believe you're right - and the one bit of documentation I've found to support this is in JLS 9.6 :

Unless explicitly modified herein, all of the rules that apply to normal interface declarations apply to annotation type declarations.

So it's basically behaving like a normal interface, where public and abstract are redundant and all fields are implicitly static and final.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM