Open In App

Java @Retention Annotations

Last Updated : 10 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, annotations are used to attach meta-data to a program element such as a class, method, instances, etc. Some annotations are used to annotate other annotations. These types of annotations are known as meta-annotations. @Retention is also a meta-annotation that comes with some retention policies. These retention policies determine at which point an annotation is discarded. There are three types of retention policies: SOURCE, CLASS, and RUNTIME.

  • RetentionPolicy.SOURCE: The annotations annotated using the SOURCE retention policy are discarded at runtime.
  • RetentionPolicy.CLASS: The annotations annotated using the CLASS retention policy are recorded in the .class file but are discarded during runtime. CLASS is the default retention policy in Java.
  • RetentionPolicy.RUNTIME: The annotations annotated using the RUNTIME retention policy are retained during runtime and can be accessed in our program during runtime.

Implementation:

Here we will be creating three custom annotations with retention policies such as SOURCE, CLASS, and RUNTIME. These custom annotations are later used to annotate three classes, namely A, B, and C. In the main method, we check if the annotations are attached to classes at runtime or not.

Example

Java




// Java Program to Illustrate Retention Annotations
 
// Importing required classes from java.lang package
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
 
// Here we will be creating 3 annotations with
// RetentionPolicy as SOURCE, CLASS, & RUNTIME
 
// Retention Annotation 1
@Retention(RetentionPolicy.SOURCE)
 
// Interface
@interface SourceRetention
{
    String value() default "Source Retention";
}
 
// Retention Annotation 2
@Retention(RetentionPolicy.CLASS)
 
// Interface
@interface ClassRetention
{
    String value() default "Class Retention";
}
 
// Retention Annotation 3
@Retention(RetentionPolicy.RUNTIME)
 
// Interface
@interface RuntimeRetention
{
    String value() default "Runtime Retention";
}
 
// Annotating classes A, B, and C
// with our custom annotations
@SourceRetention
class A {
}
 
@ClassRetention
class B {
}
 
@RuntimeRetention
class C {
};
 
// Main class
public class RetentionPolicyDemo {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Obtaining the array of annotations used to
        // annotate class A, B, and C. Array a and b will be
        // empty as their annotation are attached before
        // runtime while array c will contain the
        // RuntimeRetention annotation as it was marked with
        // RUNTIME retention policy
        Annotation a[]
            = new A().getClass().getAnnotations();
        Annotation b[]
            = new B().getClass().getAnnotations();
        Annotation c[]
            = new C().getClass().getAnnotations();
 
        // Printing the number of retained annotations of
        // each class at runtime
        System.out.println(
            "Number of annotations attached to "
            + "class A at Runtime: " + a.length);
 
        System.out.println(
            "Number of annotations attached to "
            + "class B at Runtime: " + b.length);
 
        System.out.println(
            "Number of annotations attached to "
            + "class C at Runtime: " + c.length);
 
        // Since the class C is annotated with an annotation
        //  which has retention policy as runtime so it
        // can be accessed during runtime while annotations
        // of other two classes are discarded before runtime
        // so they can't be accessed
        System.out.println(
            "Annotation attached to class C: " + c[0]);
    }
}


 
 

Output

Number of annotations attached to class A at Runtime: 0
Number of annotations attached to class B at Runtime: 0
Number of annotations attached to class C at Runtime: 1
Annotation attached to class C: @RuntimeRetention(value="Runtime Retention")

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads