A retention policy determines when to keep the annotation.
Java defines three policies:
They are captured in the java.lang.annotation.RetentionPolicy enumeration.
An annotation on a local variable declaration is not retained in the .class file.
A retention policy is set for an annotation by Java's built-in annotations: @Retention
.
The general form is shown here:
@Retention(retention-policy)
Here, retention-policy
must be one of the discussed enumeration constants.
If no retention policy is specified for an annotation, the default policy of CLASS is used.
The following version of MyAnno
uses @Retention to specify the RUNTIME retention policy.
Thus, MyAnno
will be available during the program execution.
@Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str(); int val(); }