Declaring Annotation Type Elements
The body of an annotation type declaration may contain declarations of annotation type elements. The syntax of an annotation type element declaration is shown below.
element_modifiers element_type method_name
() default
constant_expression
;
A partial declaration of the TaskInfo annotation type is repeated below, showing the declaration of the three annotation type elements at (8), (9), and (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
// …
}
Each annotation type element specifies a method declaration, resembling the abstract method declaration in a normal interface. Each annotation type element is usually referred to as an element of the annotation type. The elements declared in an annotation type declaration constitute the attributes of the annotation type.
The method declaration in an element has a return type and a method name, and is always specified with an empty formal parameter list, as exemplified by the method declarations in the elements at (8), (9), and (10) in the TaskInfo annotation type declaration. Note that no formal parameters, type parameters, or a throws clause can be specified for a method declaration in an element. The name and the return type of the method declared in an element are referred to as the name and the type of the element, respectively. Only specific types can be declared as the type of an element (p. 1561).
Annotation types are characterized by the number of elements declared in their declaration.
- A marker annotation type is an annotation type with no elements, analogous to a marker interface. A marker annotation is typically used to indicate a specific purpose that does not require any elements. Earlier in this chapter we have seen the annotation type Tag, which is a marker annotation type. The notation for applying such an annotation type to program elements is straightforward: an at-sign (@) preceding the annotation name (p. 1565)—for example, @Tag. Ample examples of marker annotation types can be found throughout this chapter.
@interface Tag {} // No element type declarations.
- A single-element annotation type, as the name suggests, is an annotation type with a single element. By convention, the method of a single-element annotation type is declared as value(). Various shorthand notations are offered when applying such annotation types to program elements (p. 1565).
@interface SecurityLevel { // Single-element type declaration
int value();
}
- A multi-element annotation type, as the name suggests, is an annotation type with more than one element. The TaskInfo annotation type is a multi-element annotation type, as it declares three elements. At the most, one method of an element in a multi-element annotation type can be declared as value(). Under certain conditions, a shorthand notation can be used when applying an annotation type declaration that has a value() element (p. 1563).