- Annotation default values is used if no value is specified.
- A default value is specified by adding a default clause.
- Default value must be of a type compatible with type.
Here is @MyAnnotation rewritten to include default values:
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String stringValue() default "defaultString";
int intValue() default 101;
}
This declaration gives a default value of "Testing" to str and 9000 to val.
This means that neither value needs to be specified when @MyAnno is used.
However, either or both can be given values if desired.
Therefore, following are the four ways that @MyAnnotation can be used:
@MyAnnotation() // both str and val default
@MyAnnotation(stringValue = "some string") // val defaults
@MyAnnotation(intValue = 100) // str defaults
@MyAnnotation(stringValue = "Testing", intValue = 100) // no defaults
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// A simple annotation type.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String stringValue() default "defaultString";
int intValue() default 101;
}
@MyAnnotation(stringValue = "for class", intValue = 100)
public class MainClass {
// Annotate a method.
@MyAnnotation(intValue = 100)
public static void myMethod() {
}
public static void main(String[] arg) {
try {
MainClass ob = new MainClass();
Method m = ob.getClass( ).getMethod("myMethod");
Annotation[] annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (Exception exc) {
}
}
}
All annotations for myMeth:
@MyAnnotation(intValue=100, stringValue=defaultString)