Java Reference
In-Depth Information
15.3. Annotating Elements
The program elements that can be annotated are all those for which
modifiers can be specified: type declarations (classes, interfaces, enums,
and annotation types), field declarations, method and constructor de-
clarations, local variable declarations, and even parameter declarations.
There is also a special mechanism to annotate packages, described in
Section 18.5 on page 476 .
As you have seen, to annotate an element you provide the name of the
annotation type being applied, preceded by @ and followed by a paren-
thesized list of initializers for each element of the annotation type. A giv-
en program element can only be annotated once per annotation type.
If the annotation type is a marker annotation or if all its elements have
default values, then the list of initializers can be omitted. For example,
you can mark a method as deprecated:
@Deprecated
public void badMethod() { /* ... */ }
Equivalently, you can just specify an empty list of initializers:
@Deprecated()
public void badMethod() { /* ... */ }
Otherwise, for each element that does not have a default value you must
list an initializer, of the form name = value , as you have seen with ClassInfo .
The order of initializers is not significant, but each element can only ap-
pear once. If an element has a default value then it need not appear, but
you can include it if you want to override the default value with a specific
one, as you saw in the Revision annotation type with ClassInfo .
 
Search WWH ::




Custom Search