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?
@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example: @interface MyAnnotation { String value(); String name(); int age(); String[] newNames(); }
If an @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7. 4. For example, this @Target meta-annotation indicates that the declared type is itself a meta-annotation type.
Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. Annotations start with @ .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With