Annotation Basics
An annotation describes code; it does not replace it
An annotation is metadata attached to a program element.
Its notation starts with @ and may appear on a class,
method, field, parameter, and other elements — if that particular annotation type allows such use.
class ReportService {
@Override
public String toString() {
return "ReportService";
}
}
Here, @Override tells the compiler that the method is intended to override another method.
If the signature is wrong and no override actually exists, the compiler reports an error.
The annotation itself does not insert a new method or execute hidden code.
Who uses annotations
Metadata can be useful at different stages of a program's life.
Some annotations are read by the compiler, others are processed by build tools or code generators,
and some are preserved until runtime and can be read by a library through reflection.
That is why the same-looking @Something notation does not by itself tell you when it will be used.
Annotations with values
An annotation can contain elements — named values supplied when the annotation is used. If an element has a default value, it may be omitted.
@SuppressWarnings("unused")
void prepareReport() {
int draftVersion = 1;
}
In this case, the string "unused" is the value of an annotation element.
Each annotation defines its own set of such elements; they are part of its declaration rather than arbitrary parameters.