Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Explain RetentionPolicy.SOURCE, CLASS and RUNTIME with one real example of each. Which is the default when @Retention is missing?

RetentionPolicy.SOURCE annotations are thrown away by the compiler, like @Override and @SuppressWarnings. CLASS annotations are written into the class file but not exposed through reflection, which suits bytecode tools and analysers such as some nullness annotations; CLASS is the default when @Retention is missing. RUNTIME annotations are kept in the class file and visible through reflection, like @Deprecated, JPA's @Entity and Spring's @Component, and any framework that reads annotations reflectively needs this retention.

Why this question is asked

Retention is the one meta-annotation that decides whether an annotation can be seen at all by the tool that needs it, and forgetting it is behind many custom annotations that mysteriously do nothing. Interviewers ask for one real example of each policy because it shows we understand who reads annotations at each stage: the compiler, tools that read class files, or code running in the JVM. In this answer, we will follow an annotation through compilation and loading, go through each policy with real examples, confirm the default, and see why a library might deliberately choose CLASS.

1A Quick Recap: Who Reads Annotations, and When

An annotation is only useful if something reads it. There are three moments when that can happen. During compilation, the compiler and annotation processors read the source. After compilation, tools that work on class files, such as static analysers, bytecode instrumenters and code shrinkers, read the compiled bytecode. At run time, our program and its frameworks read annotations through reflection, with methods such as getAnnotation and isAnnotationPresent.

@Retention chooses how far along that path the annotation travels. Each policy keeps it alive for one more stage than the previous one.

How far each retention policy travels
PolicyIn sourceIn class fileVia reflection
SOURCEYesNoNo
CLASS (the default)YesYesNo
RUNTIMEYesYesYes

2SOURCE: Only for the Compiler

A SOURCE annotation is available to the compiler and to annotation processors, then discarded. Nothing about it appears in the class file. @Override is the classic example: the compiler checks that the method really overrides something, and after that the information is useless. @SuppressWarnings is another: it only tells the compiler which warnings not to print. Lombok's annotations such as @Getter are SOURCE too, because Lombok does its work during compilation and leaves nothing behind.

Java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override { }
class Order {
@Override public String toString() { return "Order"; }
}
Order.class.getMethod("toString").isAnnotationPresent(Override.class); // false: it never reached the class file

3CLASS: In the Class File, Hidden From Reflection

A CLASS annotation is written into the class file as a so-called invisible annotation. The JVM loads the class but does not expose the annotation through the reflection API. This is the default retention: an annotation declared without @Retention behaves exactly like this.

Who uses it? Tools that read bytecode directly. Some nullness annotations, such as JetBrains' @NotNull and @Nullable, use CLASS retention (check the version you use): IntelliJ and other analysers read them from library jars to warn about null misuse in our code, and an instrumenting tool can read them to insert run-time null checks. They do not need reflection, so CLASS is enough.

Java
@interface Audited { } // no @Retention: CLASS by default
class Billing { @Audited void charge() { } }
Billing.class.getDeclaredMethod("charge").isAnnotationPresent(Audited.class); // false
// javap -v Billing.class shows it under RuntimeInvisibleAnnotations

Notice the name the class file format uses: RuntimeInvisibleAnnotations. The annotation is physically there, but invisible to code running in the JVM.

4RUNTIME: Visible to Frameworks

A RUNTIME annotation is stored as a visible annotation, and the JVM exposes it through reflection. This is what every framework that reads annotations while the application runs needs. JPA's @Entity tells Hibernate at startup which classes are entities. Spring's @Component tells component scanning which classes are beans. @Deprecated is RUNTIME as well, so tools and frameworks can see it on loaded classes.

Java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Audited { }
Billing.class.getDeclaredMethod("charge").isAnnotationPresent(Audited.class); // true

So the rule is simple: if a framework, aspect or our own code reads the annotation reflectively, it must be RUNTIME. If it is only for the compiler, SOURCE keeps the class file clean. CLASS sits in between, for tools that read bytecode.

5Follow-up questions

Why might a library deliberately choose CLASS retention?

Because its readers are tools that work on class files, not code running in the JVM. Keeping the annotation out of reflection means frameworks will not accidentally react to it, the application does not need the annotation's jar on the run-time class path for anything, and there is no reflection metadata to keep in memory. Nullness annotations are the typical case: analysers read them from compiled libraries, and nothing at run time needs to.

If a RUNTIME annotation's class is missing from the class path at run time, what happens?

Reflection simply ignores annotations whose types cannot be loaded; getAnnotations does not return them and no exception is thrown. This is why an application can depend on a library annotated with tool-specific annotations without shipping those tools.

Can retention be changed for an annotation we do not own?

No. Retention is part of the annotation type's declaration. If we need a different retention, we declare our own annotation, which is one reason some projects define their own @Nullable.

6Conclusion

In this answer, we followed an annotation through compilation, the class file and run time. SOURCE annotations such as @Override and @SuppressWarnings are discarded by the compiler. CLASS annotations, the default when @Retention is missing, are written to the class file as invisible annotations, useful to bytecode tools and analysers such as some nullness annotations. RUNTIME annotations such as @Deprecated, JPA's @Entity and Spring's @Component are visible through reflection, which any framework that reads annotations at run time requires.

What to say in the interviewAbout 70 seconds aloud

Open with

Retention decides how far an annotation travels. SOURCE annotations exist only for the compiler and annotation processors and are thrown away after compilation; @Override and @SuppressWarnings are the standard examples, and Lombok's annotations work the same way.

Then, if they want more

CLASS annotations are written into the class file, but the JVM does not expose them through reflection; javap shows them as runtime-invisible annotations. That is the default if @Retention is missing, which surprises a lot of people.

They are useful for tools that read bytecode, like static analysers or instrumenters; some nullness annotations, such as JetBrains' @NotNull, use CLASS retention so IntelliJ can read them from library jars. RUNTIME annotations are in the class file and visible through reflection, so frameworks can read them while the application runs: @Deprecated, JPA's @Entity and Spring's @Component are all RUNTIME.

The practical rule is that anything a framework, an aspect or my own code reads reflectively must be RUNTIME, otherwise isAnnotationPresent just returns false. A library might deliberately pick CLASS when its only readers are bytecode tools, so frameworks never react to it and nothing is kept for reflection.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain RetentionPolicy.SOURCE, CLASS and RUNTIME with one real example of each. Which...": https://scrivio-interview-library.netlify.app/java/annotations/ae6670ebca/