Retention

@Retention is a meta-annotation in Java that is used to indicate how long annotations with this annotation are to be retained.It is defined in the java.lang.annotation package and is applied to other annotations. This helps the compiler or JVM understand whether the annotation should be discarded at compile time, retained in the class file, or be available at runtime via reflection.

Three Retention Policies:

Simple Program

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Annotation;

// Define custom annotation with RUNTIME retention
@Retention(RetentionPolicy.RUNTIME)
@interface Developer {
    String name();
    String date();
}

@Developer(name = "LotusJavaPrince", date = "2025-05-25")
public class SimpleRetentionExample {
    public static void main(String[] args) {
        Class<SimpleRetentionExample> obj = SimpleRetentionExample.class;

        // Read the annotation using reflection
        if (obj.isAnnotationPresent(Developer.class)) {
            Developer dev = obj.getAnnotation(Developer.class);
            System.out.println("Developer: " + dev.name());
            System.out.println("Date: " + dev.date());
        }
    }
}

Output

Developer: LotusJavaPrince
Date: 2025-05-25Code language: HTTP (http)

Problem Statement:

Mahesh, the system administrator, has to audit classes in a banking application developed by LotusJavaPrince. LotusJavaPrince marks all classes developed by him using a custom @AuditInfo annotation. Mahesh wants to write a program that lists all such classes at runtime, so he can keep track of who developed which module. Use @Retention(RetentionPolicy.RUNTIME) to retain annotation data during execution for reflection-based auditing.

Solution Strategy:

  • Create a custom annotation @AuditInfo
  • Annotate classes with developer name and version
  • Write a runtime program to scan annotated classes and log details
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Annotation;

// Step 1: Create annotation
@Retention(RetentionPolicy.RUNTIME)
@interface AuditInfo {
    String developer();
    String version();
}

// Step 2: Annotate business classes
@AuditInfo(developer = "LotusJavaPrince", version = "1.0")
class LoanProcessor {
    void processLoan() {
        System.out.println("Processing loan...");
    }
}

@AuditInfo(developer = "LotusJavaPrince", version = "2.0")
class AccountManager {
    void manageAccount() {
        System.out.println("Managing account...");
    }
}

class TransactionHandler {
    void handleTransaction() {
        System.out.println("Handling transaction...");
    }
}

// Step 3: Mahesh's Auditor Tool
public class AuditorTool {
    public static void main(String[] args) {
        Class<?>[] classes = { LoanProcessor.class, AccountManager.class, TransactionHandler.class };

        System.out.println("---- Audit Report ----");
        for (Class<?> cls : classes) {
            if (cls.isAnnotationPresent(AuditInfo.class)) {
                AuditInfo info = cls.getAnnotation(AuditInfo.class);
                System.out.println("Class: " + cls.getSimpleName());
                System.out.println("  Developer: " + info.developer());
                System.out.println("  Version: " + info.version());
                System.out.println("----------------------");
            }
        }
    }
}

Output:

---- Audit Report ----
Class: LoanProcessor
  Developer: LotusJavaPrince
  Version: 1.0
----------------------
Class: AccountManager
  Developer: LotusJavaPrince
  Version: 2.0
----------------------Code language: CSS (css)

The @Retention annotation in Java plays a crucial role in determining how long other annotations are retained in the program lifecycle. By specifying one of the three retention policies—SOURCE, CLASS, or RUNTIME—developers can control whether an annotation is:

  • Only visible in source code (discarded during compilation),
  • Stored in the class file (but not available during runtime), or
  • Available at runtime through reflection (most commonly used in frameworks and enterprise applications).
Scroll to Top