Java Annotations — Interview Questions
Reviewed by Gurusankar M.
Short Answer
Annotations are typed metadata attached to code. @Retention decides how long they survive — SOURCE (compile only, e.g. @Override), CLASS (in bytecode, not at runtime), or RUNTIME (readable via reflection). @Target limits where they apply. They're processed two ways: at compile time by annotation processors (Lombok, MapStruct, Dagger generate code), or at runtime via reflection (Spring, JUnit, Jackson read them to drive behaviour).
On this page
Concept Made Simple
Coffee Chat Question
“How do Java annotations work — retention policies, targets, and how are they processed?”
Remember It Faster
Mind Map Answer
- @Retention SOURCE
- Discarded after compile (@Override)
- @Retention CLASS
- In .class, not visible at runtime (default)
- @Retention RUNTIME
- Readable via reflection (@Autowired)
- @Target
- Where it's allowed (TYPE, METHOD, FIELD…)
Two processing worlds: compile-time annotation processors (APT) generate code or fail the build (Lombok, MapStruct, Dagger); runtime frameworks reflect over RUNTIME-retention annotations to configure themselves (Spring's @Component, JUnit's @Test, Jackson's @JsonProperty).
A custom annotation is just an @interface with elements; you add meta-annotations (@Retention, @Target) to control its lifetime and placement, then read it with reflection or a processor.
Key takeaway: retention decides *who can read it* — pick RUNTIME only if a framework reflects over it; otherwise SOURCE/CLASS keeps it out of the runtime.
Learn by Doing
Hands-on Keyboard
@Retention(RetentionPolicy.RUNTIME) // visible to reflection
@Target(ElementType.METHOD)
@interface Audited { String value() default ""; }
class Service {
@Audited("payment")
void pay() { }
}
// A framework reads it at runtime:
Method m = Service.class.getMethod("pay");
Audited a = m.getAnnotation(Audited.class);
if (a != null) System.out.println("audit: " + a.value());audit: payment
Think Beyond the Expected
What If?
You wrote a custom annotation but reflection returns null for it at runtime — why?
Almost certainly the retention policy. The default is RetentionPolicy.CLASS, which keeps the annotation in the .class file but does NOT make it visible to reflection — so getAnnotation() returns null. Add @Retention(RetentionPolicy.RUNTIME) so the annotation survives into the runtime constant pool where reflection can read it. If you only need compile-time processing, use an annotation processor instead of reflection.
Real World
Spring (@Component/@Autowired/@Transactional), JUnit (@Test), Jackson (@JsonProperty), and Bean Validation (@NotNull) all read RUNTIME annotations reflectively; Lombok, MapStruct, and Dagger use SOURCE-level processors to generate boilerplate at compile time — no runtime cost. Choosing the wrong retention is a frequent cause of 'my annotation does nothing.'
Interviewer's Expectation
Keywords they're listening for:
- metadata on code
- SOURCE/CLASS/RUNTIME retention
- @Target restricts placement
- compile-time processors vs runtime reflection
- RUNTIME needed for reflective frameworks
Common Mistakes
- Leaving retention at the default when reflection needs RUNTIME
- Reading a SOURCE-retained annotation at runtime
- Over-annotating instead of using plain configuration
Best Practices
- Set RUNTIME retention only when a framework reflects over it
- Constrain placement with @Target
- Prefer compile-time processors for zero runtime overhead
Follow-up Questions
- When would you use an annotation processor over reflection?
- What are meta-annotations and repeatable annotations?
- How does Bean Validation use annotations at runtime?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Annotations (Advanced Java) Interview question: "How do Java annotations work — retention policies, targets, and how are they processed?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair