Java Retention Policies - Interview Questions & Answers

Master Java Retention Policies with interview-focused questions and answers. Learn SOURCE, CLASS, RUNTIME retention policies, meta-annotations, reflection, and Spring Boot internals with practical Java examples.


Introduction

Retention Policy is one of the most frequently asked annotation topics in Java interviews.

When developers create custom annotations, they must decide how long the annotation should be available. This decision is controlled by the @Retention meta-annotation.

Spring Boot, Hibernate, JUnit, and many enterprise frameworks rely on RetentionPolicy.RUNTIME because they discover annotations using Reflection.


Why Interviewers Ask About Retention Policies?

Interviewers want to evaluate whether you understand:

  • Annotation lifecycle
  • Compile-time vs Runtime processing
  • Reflection
  • Framework internals
  • Spring Boot architecture

Understanding retention policies helps explain how frameworks process annotations behind the scenes.

flowchart LR

SourceCode --> Compiler

Compiler --> ClassFile

ClassFile --> JVM

JVM --> Reflection

Reflection --> Framework

Interview Question 1

What is a Retention Policy?

Answer

A Retention Policy defines how long an annotation remains available during the application's lifecycle.

It determines whether an annotation exists:

  • Only in source code
  • Inside compiled class files
  • At runtime

Java provides three retention policies:

  • SOURCE
  • CLASS
  • RUNTIME

Diagram

flowchart TD

RetentionPolicy --> SOURCE

RetentionPolicy --> CLASS

RetentionPolicy --> RUNTIME

Java Example

@Retention(RetentionPolicy.RUNTIME)
public @interface Audit {

}

Interview Tip

A common interview answer:

Retention Policy controls the lifetime of an annotation.


Interview Question 2

What is the @Retention Annotation?

Answer

@Retention is a Meta Annotation.

It specifies which retention policy should be used for a custom annotation.

Syntax:

@Retention(RetentionPolicy.RUNTIME)
public @interface Audit {

}

Without @Retention, Java uses the default retention policy.


Diagram

flowchart LR

@Target

@Retention --> CustomAnnotation

CustomAnnotation --> JavaClass

Interview Tip

Remember:

@Retention is applied to another annotation, not to classes or methods directly.


Interview Question 3

What is RetentionPolicy.SOURCE?

Answer

SOURCE retention means the annotation exists only in the source code.

The compiler removes it during compilation.

It is not available in:

  • .class files
  • JVM
  • Reflection

Java Example

@Retention(RetentionPolicy.SOURCE)
public @interface Author {

}

Diagram

flowchart LR

SourceCode --> SOURCE

SOURCE --> Compiler

Compiler --> Removed

Common Use Cases

  • Code generation
  • Compiler checks
  • Static analysis
  • IDE support

Examples:

  • @Override
  • @SuppressWarnings

Interview Tip

Reflection cannot read SOURCE annotations.


Interview Question 4

What is RetentionPolicy.CLASS?

Answer

CLASS retention stores annotations inside the compiled .class file.

However, the JVM does not retain them during runtime.

Reflection cannot access CLASS annotations.

This is also the default retention policy if none is specified.


Java Example

@Retention(RetentionPolicy.CLASS)
public @interface Version {

}

Diagram

flowchart LR

Source --> Compiler

Compiler --> ClassFile

ClassFile --> JVM

JVM --> Removed

Common Use Cases

  • Bytecode processing
  • Build tools
  • Static analysis
  • Code instrumentation

Interview Tip

Many developers mistakenly believe CLASS annotations are available through Reflection.

They are not.


Interview Question 5

What is RetentionPolicy.RUNTIME?

Answer

RUNTIME retention keeps annotations available throughout the entire application lifecycle.

The annotation remains available:

  • Source Code
  • Class File
  • JVM Runtime
  • Reflection

This is the most commonly used retention policy in enterprise applications.


Java Example

@Retention(RetentionPolicy.RUNTIME)
public @interface Audit {

}

Diagram

flowchart LR

Source --> Compiler

Compiler --> ClassFile

ClassFile --> JVM

JVM --> Reflection

Reflection --> SpringBoot

Enterprise Examples

Spring Boot uses RUNTIME retention for:

  • @Component
  • @Service
  • @Repository
  • @Controller
  • @RestController
  • @Transactional

Interview Tip

If an annotation needs to be discovered by Spring Boot or Reflection, always choose:

RetentionPolicy.RUNTIME


Interview Question 6

What is the Difference Between SOURCE, CLASS, and RUNTIME?

Answer

The major difference is how long the annotation is retained.

Retention Policy Source Code Class File Runtime Reflection
SOURCE
CLASS
RUNTIME

Diagram

flowchart LR

SourceCode --> SOURCE

SourceCode --> CLASS

SourceCode --> RUNTIME

SOURCE --> RemovedAfterCompile

CLASS --> ClassFileOnly

RUNTIME --> JVMRuntime

JVMRuntime --> Reflection

Interview Tip

Remember:

  • SOURCE → Compiler only
  • CLASS → Compiler + Class File
  • RUNTIME → Compiler + JVM + Reflection

Interview Question 7

Why does Spring Boot use RetentionPolicy.RUNTIME?

Answer

Spring Boot scans classes at runtime using Reflection.

To detect annotations like:

  • @Component
  • @Service
  • @Repository
  • @RestController
  • @Transactional

the annotations must still exist after the application starts.

Only RetentionPolicy.RUNTIME satisfies this requirement.


Diagram

flowchart LR

SpringApplication --> ComponentScan

ComponentScan --> Reflection

Reflection --> "@Component"

Reflection --> "@Service"

Reflection --> IoCContainer

IoCContainer --> DependencyInjection

Java Example

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Audit {

}

Interview Tip

A very common interview question:

Why doesn't Spring use CLASS retention?

Answer:

Because CLASS annotations disappear before Reflection executes.


Interview Question 8

How do you read Runtime Annotations using Reflection?

Answer

Reflection reads annotations during application execution.


Java Example

@Retention(RetentionPolicy.RUNTIME)
@interface Audit {

    String module();

}

class PaymentService {

    @Audit(module = "Payment")
    public void transfer() {

    }

}

Reading annotation:

Method method = PaymentService.class.getMethod("transfer");

Audit audit = method.getAnnotation(Audit.class);

System.out.println(audit.module());

Output

Payment

Diagram

flowchart LR

JavaClass --> Reflection

Reflection --> RuntimeAnnotation

RuntimeAnnotation --> BusinessLogic

Interview Tip

Reflection only works with annotations retained at RUNTIME.


Interview Question 9

What are the Common Mistakes with Retention Policies?

Answer

Developers often make these mistakes:

❌ Forgetting to specify @Retention

❌ Using SOURCE for Spring annotations

❌ Using Reflection on CLASS annotations

❌ Assuming all annotations are available at runtime

❌ Not understanding the default retention policy


Diagram

flowchart TD

Mistakes --> MissingRetention

Mistakes --> WrongPolicy

Mistakes --> ReflectionFailure

Mistakes --> RuntimeErrors

Interview Tip

If your annotation is intended for Spring Boot, Hibernate, or any runtime framework, always verify that it uses:

RetentionPolicy.RUNTIME

Interview Question 10

What are the Best Practices for Choosing a Retention Policy?

Answer

Use the appropriate retention policy based on the use case.

Use Case Recommended Policy
Compiler Validation SOURCE
Static Analysis SOURCE
Bytecode Processing CLASS
Runtime Frameworks RUNTIME
Spring Boot RUNTIME
Reflection RUNTIME

Diagram

flowchart LR

Requirement --> CompilerCheck

Requirement --> BytecodeProcessing

Requirement --> RuntimeFramework

CompilerCheck --> SOURCE

BytecodeProcessing --> CLASS

RuntimeFramework --> RUNTIME

Interview Tip

Choose the lowest retention level that satisfies the requirement.

Don't use RUNTIME unless the annotation must be available after the application starts.


Common Interview Mistakes

  • Confusing @Retention with @Target
  • Assuming all annotations are available at runtime
  • Forgetting that CLASS is the default retention policy
  • Using Reflection with SOURCE annotations
  • Choosing RUNTIME when it isn't needed
  • Not explaining why Spring Boot requires runtime annotations

Quick Revision

Concept Key Point
@Retention Defines annotation lifetime
SOURCE Exists only in source code
CLASS Stored in .class file
RUNTIME Available during program execution
Reflection Reads only RUNTIME annotations
Spring Boot Uses RUNTIME annotations
Default Policy CLASS
Best Practice Choose the lowest suitable retention level

Interviewer's Expectations

Junior Java Developer

  • Know all three retention policies.
  • Explain basic differences.
  • Identify common use cases.

Senior Java Developer

  • Explain annotation lifecycle.
  • Demonstrate Reflection.
  • Explain why Spring Boot uses RUNTIME.
  • Compare retention policies with examples.

Solution Architect

  • Explain framework internals.
  • Discuss performance implications of runtime scanning.
  • Choose appropriate retention policies for framework design.
  • Explain trade-offs between compile-time and runtime processing.

Related Interview Questions

  • What is @Target?
  • What is Reflection in Java?
  • How does Spring Boot scan annotations?
  • Difference between @Component and @Bean?
  • How does @Autowired work internally?
  • What are Meta Annotations?
  • How do Annotation Processors work?
  • What is Compile-Time Annotation Processing?

Summary

Retention Policies determine how long annotations remain available during the lifecycle of a Java application. Understanding the differences between SOURCE, CLASS, and RUNTIME is essential for building custom annotations and understanding how frameworks such as Spring Boot and Hibernate work internally.

For interviews, don't just memorize the definitions. Be prepared to explain when to use each policy, how Reflection interacts with runtime annotations, why Spring Boot depends on RetentionPolicy.RUNTIME, and what trade-offs each option provides. This demonstrates a strong understanding of Java internals and enterprise application development.