Java Reference
In-Depth Information
15.2. Annotation Types
Annotation types are a special kind of interface, declared, as you have
seen, with the interface keyword preceded by the @ character. Annotation
types can be declared anywhere an interface can be declaredthat is, as a
top-level annotation type or nested within another typeand can have the
same modifiers applied as interfaces. Characterizing annotation types as
interfaces is a little misleading, however, as aside from borrowing some
syntax and some associated usage rules, annotation types bear little re-
semblance to interfaces in normal use. [2]
[2] Except when accessed reflectively at runtimesee " Annotation Queries " on page 414 .
The methods declared in an annotation type are known as the elements
of the annotation type. These elements are constrained by strict rules:
The type of the element can only be a primitive type, String , an
enum type, another annotation type, Class (or a specific generic
invocation of Class ), or an array of one of the preceding types.
The element cannot declare any parameters.
The element cannot have a throws clause.
The element cannot define a type parameter (that is, it can't be a
generic method).
In essence, the elements of an annotation type are like the fields of an
object, that is instantiated for each program element the annotation type
is applied to. The values of these fields are determined by the initializers
given when the annotation is applied or by the default value of the ele-
ment if it has one.
You can give an element a default value by following the empty para-
meter list with the keyword default and a suitable value. For example,
suppose you want to represent a revision number as a pair of integers for
 
 
Search WWH ::




Custom Search