Defaults for Annotation Type Elements
An annotation type element declaration can declare a default value for an element using the optional default clause in the annotation type element declaration, as shown in the declaration of the MultiElementAnnotationType earlier. The default value is specified in the default clause as a constant expression—that is, the operands of the expression are all constants so that the compiler is able to determine its value at compile time. The constant expression must not be null. The declaration of the Refactor annotation type below shows examples of illegal default values.
@interface Refactor {
int id() default (int) (Math.random() * 10);// Not a const expression.
String value() default null; // Cannot be null.
String deadline() default new String(“2021-01-11”); // Not a const expression.
String[] team() default new String[] {“VJ”, “PT”}; // Not a const expression.
}
An element that specifies a default value is called an optional annotation element. An element that does not specify a default clause is called a required annotation element. As we shall see when an annotation is applied to a program element, specifying a value for an optional annotation element can be omitted, in which case the default value in the declaration of the annotation type element is used. For a required annotation element, a value for the element must be specified when the annotation is applied to a program element. We see that the declaration of the TaskInfo annotation type below has two required annotation elements, at (8) and at (9), and one optional annotation element at (10).
// …
public @interface TaskInfo { // (7)
String taskDesc(); // (8) Required annotation element
String[] assignedTo(); // (9) Required annotation element
TaskPriority priority() default TaskPriority.NORMAL; // (10) Optional
// annotation element
// …
}
25.3 Applying Annotations
Once an annotation type has been compiled, it can be applied to program elements. An annotation is an application of an annotation type to a program element, and we refer to the annotation as being of that type. Which program elements an annotation can be applied to is typically determined by the meta-annotation @Target specified in the declaration of the annotation type (p. 1569). Each annotation refers to a specific invocation of an annotation type. An annotation is always specified with the at-sign (@) preceding the name of the annotation type, and usually provides values for the elements of the annotation type.
Multiple annotations of different types can be applied to a program element. Repeatable annotations are covered later (p. 1575).
Annotations can be characterized according to the notation that can be used when applied to a program element. We distinguish between three kinds of annotations:
- Normal annotations
- Marker annotations
- Single-element annotations
The last two annotations are special cases of normal annotations. Choosing which annotation to use depends on knowing what annotation type elements are declared in an annotation type declaration. The different kinds of annotations are discussed in this section.