@Retention is a meta-annotation used to define how long a custom annotation should remain available during the application's lifecycle. It determines whether an annotation is retained only in source code, stored in the compiled class file, or accessible at runtime through reflection.
Frameworks such as Spring, Hibernate, and JUnit rely on annotations with RUNTIME retention because they inspect annotations through reflection while the application is running.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface Developer {
String name();
}
@Developer(name = "John")
class Employee {
}
public class Main {
public static void main(String[] args) {
Developer annotation =
Employee.class.getAnnotation(
Developer.class);
System.out.println(
annotation.name());
}
}A concise interview answer is: @Retention specifies how long an annotation should be retained in Java. SOURCE keeps it only in source code, CLASS stores it in bytecode, and RUNTIME makes it available during execution through reflection, which is commonly used by frameworks and libraries.
Last updated: 16 July 2026
Previous question: How would the introduction of default methods in interfaces with Java 8 affect design decisions between using an interface and an abstract class?
Next question: What does the @Target annotation do?