Java Reference
In-Depth Information
1.15. Annotations
Annotations provide information about your program, or the elements
of that program (classes, methods, fields, variables, and so forth), in a
structured way that is amenable to automated processing by external
tools. For example, in many organizations all code that gets written must
be reviewed by a programmer other than the author. Keeping track of
what code has been reviewed, by whom, and when requires a lot of book-
keeping and is a task most suited to an automated tooland indeed some
code management systems will support this kind of task. Annotations al-
low you to provide this review information with the code that is being re-
viewed. For example, here is how you could annotate a class with review
information:
@Reviewed(reviewer = "Joe Smith", date = 20050331)
public class Point {
// ...
}
An annotation is considered a modifier (like
public
or
static
) and should
appear before other modifiers, on a line of its own. It is indicated by
an
@
character followed by the name of an
annotation type
in this case
Reviewed
. An annotation type is a special kind of interface, whose mem-
bers are known as
elements.
Here is the definition of our
Reviewed
an-
notation type:
@interface Reviewed {
String reviewer();
int date();
}
This is very similar to an interface declaration, except the
interface
keyword is again preceded by the
@
character. When you apply an an-
notation to a program element, you supply values for all of the elements