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).
β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.