Java Reference
In-Depth Information
You can also omit the braces if you specify only one element in the array for the value element of the Reviewers
annotation type.
@Reviewers("John Jacobs")
public class Test {
// Code goes here
}
You just saw several examples using the name of the element as value . Here is the general rule of omitting
the name of the element in an annotation: if you supply only one value when using an annotation, the name of the
element is assumed value . This means that you are not required to have only one element in the annotation type,
which is named value , to omit its name in the annotations. If you have an annotation type, which has an element
named value (with or without a default value) and all other elements have default values, you can still omit the name
of the element in annotation instances of this type. Here are some examples to illustrate this rule:
public @interface A {
String value();
int id() default 10;
}
// Same as @A(value="Hello", id=10)
@A("Hello")
public class Test {
// Code goes here
}
// Won't compile. Must use only one value to omit the element name
@A("Hello", id=16)
public class WontCompile {
// Code goes here
}
// OK. Must use name=value pair when passing more than one value
@A(value="Hello", id=16)
public class Test {
// Code goes here
}
Marker Annotation Types
A marker annotation type is an annotation type that does not declare any elements, not even one with a default value.
Typically, a marker annotation is used by the annotation processing tools, which generate boilerplate code based on
the marker annotation type.
public @interface Marker {
// No element declarations
}
 
Search WWH ::




Custom Search