Sub topic 8
Declaring and applying annotations
This chapter assumes you know Java classes, interfaces, and enums, and that you can compile and run a program. An annotation is metadata attached to a program element. It does not change what the code does by itself. Tools, the compiler, or reflection code read it and act on it.
Annotations you already know
You have likely used @Override and @Deprecated. @Override makes the compiler check that a method really overrides one from a supertype. @Deprecated marks an element that callers should avoid. Both are declared with the same syntax you will use for your own annotations.
Declaring an annotation type
An annotation type is declared with @interface. Its members are written like abstract methods with no parameters. These members are called elements.
public @interface Task {
String owner();
int priority() default 3;
String[] tags() default {};
}Rules for elements:
- The return type must be a primitive,
String,Class, an enum, another annotation, or a one-dimensional array of one of these. - An element can have a
defaultvalue. The default must be a compile-time constant, andnullis not allowed. - An element without a default must be given a value wherever the annotation is applied.
- Elements cannot take parameters or declare
throws.
Applying an annotation
Write @ and the type name before the element, then give values as name = value pairs.
@Task(owner = "ada", priority = 1, tags = {"core", "urgent"})
public class Report {
@Task(owner = "grace")
public void run() {
System.out.println("running");
}
public static void main(String[] args) {
new Report().run();
}
}The second use omits priority and tags, so they take their defaults. A single-element array may drop its braces, so tags = "core" is legal.
The `value` shorthand
An element named value has a special rule. If it is the only element you set, you can leave out the name.
public @interface Label {
String value();
}
@Label("account")
class Account { }@Label("account") means the same as @Label(value = "account"). This is why many annotations name their main element value.
Marker annotations
An annotation with no elements is a marker. Applying it needs no parentheses, as in @Override.
public @interface Internal { }
@Internal
class Helper { }Where annotations can go
By default you can place an annotation on declarations: classes, interfaces, enums, fields, methods, constructors, parameters, local variables, and packages. Limiting an annotation to specific kinds of targets is done with a meta-annotation, covered in the next chapter. You can also stack several different annotations on one element.
@Internal
@Label("helper")
class Util { }Using the same annotation type twice on one element is an error unless it is declared repeatable, which also comes later.
What the compiler checks
The compiler verifies that every required element has a value, that each value matches its element type, and that names are spelled correctly. A misspelled element name or a missing owner is a compile error.
Exercise
Declare an annotation Version with a required int value() and an optional String note() defaulting to an empty string. Show two valid uses: one that sets only the version, and one that sets both.
Show answer
public @interface Version { int value(); String note() default ""; } Valid uses: @Version(2) and @Version(value = 2, note = "rewritten"). The first uses the value shorthand because value is the only element set; the second must name both elements because it sets more than one.