The Retention meta-annotation type sets how an annotation type should be retained by Java.
An annotation can be retained at three levels.
A "source code only" retention policy will remove annotation during compiling.
"class file only" annotation cannot be read at runtime.
"class file and runtime" annotation instances are available for reading at runtime.
The Retention meta-annotation level is controlled by the java.lang.annotation.RetentionPolicy enum type.
RetentionPolicy enum has three constants
Constant | Level |
---|---|
SOURCE | source only |
CLASS | class only |
RUNTIME | class-and-runtime |
The following code uses the Retention meta-annotation on the Version annotation type.
It sets that the Version annotations should be available at runtime.
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface Version { int major(); int minor(); }