Java Annotation retention policy

Introduction

A retention policy determines when to keep the annotation.

Java defines three policies:

  • SOURCE - retained only in the source file, discarded during compilation
  • CLASS - stored in the .class file during compilation, not available during run time
  • RUNTIME - stored in the .class file during compilation and available during run time

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();  
} 



PreviousNext

Related