Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 760
ROMANIA_engineer Avatar asked Apr 24 '15 14:04

ROMANIA_engineer


People also ask

What is @interface annotation in Java?

@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(); }

What is @target annotation in Java?

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.

What is use of annotation in Java?

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 @ .


1 Answers

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.

like image 63
Jon Skeet Avatar answered Oct 23 '22 01:10

Jon Skeet