Annotations
Annotations
provide a way to associate arbitrary information or
metadata
with program elements.
Syntactically, annotations are used
like modifiers and can be applied to the declarations of packages,
types, constructors, methods, fields, parameters, and local
variables. The information stored in an annotation takes the form of
name
=
value
pairs, whose type is specified by the annotation
type. The annotation type is a kind of interface that also
serves to provide access to the annotation through the Java
Reflection API.
Annotations can be used to associate any kind of information you want
with a program element. The only fundamental rule is that an
annotation cannot affect the way the program runs: the code must run
identically even if you add or remove annotations. Another way to say
this is that the
Java interpreter ignores annotations
(although it does make
“runtime-visible” annotations
available for reflective access through the Java Reflection API).
Since the Java VM ignores annotations, an annotation type is not
useful unless accompanied by a tool that can do something with the
information stored in annotations of that type. In this chapter
we’ll cover standard annotation and meta-annotation
types like Override and Target.
The tool that accompanies these types is the Java compiler, which
must process them in certain ways (as we’ll describe
later in this section).
It is easy to imagine any number of other uses for annotations.[7] A local variable might be annotated ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access