An annotation interface declaration specifies an annotation interface, a specialized kind of interface. To distinguish an annotation interface declaration from a normal interface declaration, the keyword interface is preceded by an at sign (@).
AnnotationInterfaceDeclaration:
{InterfaceModifier} @ interface TypeIdentifier AnnotationInterfaceBody
Note that the at sign (@) and the keyword interface are distinct tokens. It is possible to separate them with whitespace, but this is discouraged as a matter of style.
Unless explicitly modified in this section and its subsections, all of the rules that apply to normal interface declarations (§9.1) apply to annotation interface declarations.
For example, annotation interface declarations have the same rules for scope as normal interface declarations.
It is a compile-time error if an annotation interface declaration has the modifier sealed or non-sealed (§9.1.1.4).
An annotation interface declaration may specify a top level interface or a member interface, but not a local interface (§14.3).
An annotation interface declaration is not permitted syntactically to appear within a block, by virtue of the LocalClassOrInterfaceDeclaration production in §14.3.
It is a compile-time error if an annotation interface declaration appears directly or indirectly in the body of a local class, local interface, or anonymous class declaration (§14.3, §15.9.5).
This rule, together with the syntactic restriction on annotation interface declarations noted above, ensures that an annotation interface always has a canonical name (§6.7). Having such a name is important because the purpose of an annotation interface is to be used by annotations in other compilation units. Since a local class or interface does not have a canonical name, an annotation interface declared anywhere within its syntactic body (if that were allowed) would not have a canonical name either.
The following code shows the effect of this rule and the related syntactic restriction:
class C {
@interface A1 {} /* Legal: an annotation interface can be a
member interface */
void m() {
@interface A2 {} /* Illegal: an annotation interface cannot
be a local interface */
class D {
@interface A3 {} /* Illegal: an annotation interface
cannot be specified anywhere within
the body of local class D */
class E {
@interface A4 {}
/* Illegal: an annotation interface cannot be
specified anywhere within the body of local class
D, even as a member of a class E nested in D */
}
}
}
}
An annotation interface is never generic (§9.1.2).
Unlike a normal interface declaration, an annotation interface declaration cannot declare any type variables, by virtue of the AnnotationTypeDeclaration production.
The direct superinterface type of an annotation interface is always java.lang.annotation.Annotation (§9.1.3).
Unlike a normal interface declaration, an annotation interface declaration cannot choose the direct superinterface type via an extends clause, by virtue of the AnnotationTypeDeclaration production.
A consequence of the fact that an annotation interface declaration does not explicitly specify a superinterface type via extends is that a subinterface of an annotation interface is never itself an annotation interface, since the subinterface's declaration necessarily uses an extends clause. Similarly, java.lang.annotation.Annotation is not itself an annotation interface.
An annotation interface inherits several methods from java.lang.annotation.Annotation, including the implicitly declared methods corresponding to the instance methods of Object (§9.2), yet these methods do not define elements of the annotation interface (§9.6.1).
Because these methods do not define elements of the annotation interface, it is illegal to use them in annotations conforming to the annotation interface (§9.7). Without this rule, we could not ensure that elements were of the types representable in annotations, or that accessor methods for them would be available.
Sections: