Java Annotations — Interview Questions
⚡ 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).
☕Coffee Chat Question
Concept Made Simple
“How do Java annotations work — retention policies, targets, and how are they processed?”
🧠Mind Map Answer
Remember It Faster
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.
⌨️Hands-on Keyboard
Learn by Doing
@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
🔥What If?
Think Beyond the Expected
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:
⚠️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
- 1When would you use an annotation processor over reflection?
- 2What are meta-annotations and repeatable annotations?
- 3How does Bean Validation use annotations at runtime?
🧩Related Technologies
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?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.