Quick revision
The short answer
Meta-annotations are annotations placed on the declaration of another annotation type, describing how that annotation behaves. @Retention says how long it survives (source only, class file, or run time); @Target says where it may be placed; @Documented makes it appear in the Javadoc of the elements it annotates; @Inherited makes a class-level annotation visible on subclasses through getAnnotation; and @Repeatable names a container annotation so the same annotation can appear more than once on one element.
What to remember
In this answer, we saw that meta-annotations are annotations placed on annotation type declarations. @Retention chooses SOURCE, CLASS (the default) or RUNTIME; @Target limits where the annotation may be used; @Documented shows it in the Javadoc of annotated elements; @Inherited lets a class-level annotation pass to subclasses through getAnnotation, but never from interfaces or onto methods, because multiple interfaces would make it ambiguous; and @Repeatable names a container so the annotation can appear several times, read back with getAnnotationsByType.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Meta-annotations are annotations placed on the declaration of another annotation type, describing how that annotation behaves. @Retention says how long it survives (source only, class file, or run time); @Target says where it may be placed; @Documented makes it appear in the Javadoc of the elements it annotates; @Inherited makes a class-level annotation visible on subclasses through getAnnotation; and @Repeatable names a container annotation so the same annotation can appear more than once on one element.
Why this question is asked
Every time we write our own annotation, the first lines above @interface are meta-annotations, and getting them wrong produces some of the most confusing bugs in Java: an annotation that a framework simply cannot see, or one that silently does not apply to a subclass. Interviewers ask about them to check that we know what each one controls before we build anything on custom annotations. In this answer, we will define meta-annotations, go through the five standard ones with a small example each, and see why @Inherited deliberately ignores interfaces.
1What a Meta-Annotation Is
An annotation type is declared with @interface. Like a class, that declaration can itself carry annotations, and those are called meta-annotations: annotations about an annotation. They do not describe our business code; they describe the annotation, telling the compiler and the JVM where the annotation may appear, whether it is kept after compilation, and how tools should treat it.
The standard ones live in java.lang.annotation. Here is a custom annotation that uses all five, which we will take apart one by one.
2@Retention and @Target
@Retention takes one RetentionPolicy. SOURCE means the compiler discards the annotation after compiling, as with @Override. CLASS means it is written into the class file but the JVM does not expose it through reflection; this is the default when @Retention is missing. RUNTIME means it is in the class file and visible through reflection, which is what any framework that reads annotations while the program runs needs.
@Target lists the kinds of elements where the annotation may be placed, as ElementType values such as TYPE (classes, interfaces, enums, records), METHOD, FIELD, PARAMETER, CONSTRUCTOR, and TYPE_USE for any use of a type. If @Target is missing, the annotation may be used on almost any declaration. Putting it in the wrong place with a @Target present is a compile error, which is exactly the point: misuse fails early.
3@Documented
By default, the Javadoc tool does not show annotations in the generated documentation of the elements that carry them. @Documented changes that: if Owner is @Documented, the Javadoc page for InvoiceService shows @Owner(team = "payments") in its signature. Use it when the annotation is part of the element's contract that readers of the docs should know about, such as @Deprecated or @FunctionalInterface, both of which are themselves @Documented. It has no effect on compilation or run time.
4@Inherited and Why It Ignores Interfaces
Normally an annotation on a class belongs to that class only. If Owner is @Inherited and placed on a superclass, then calling getAnnotation(Owner.class) on a subclass that has no @Owner of its own returns the superclass's annotation.
| Annotation placed on | Visible on | Inherited? |
|---|---|---|
| Superclass | Subclass via getAnnotation | Yes |
| Interface | Implementing class | No |
| Method | Overriding method | No |
Superclass, annotation without @Inherited | Subclass | No |
There are two important limits. @Inherited only works for annotations on classes, through the superclass chain; annotations on interfaces are never inherited by implementing classes, and annotations on methods are never inherited by overriding methods. The reason for the interface rule is ambiguity. A class has exactly one superclass but may implement many interfaces. If two interfaces carried @Owner with different teams, which one should the class inherit? Rather than invent a rule for conflicts, Java's designers limited inheritance to the single, unambiguous superclass chain. Frameworks like Spring work around this with their own search utilities that do look at interfaces and methods.
5@Repeatable
Before Java 8, an annotation could appear at most once on an element. @Repeatable lifts that limit. We name a container annotation, whose value() is an array of our annotation, and the compiler wraps repeated uses into the container for us.
Notice the catch: with two uses, getAnnotation(Owner.class) returns null, because what is really stored is @Owners. getAnnotationsByType looks through the container and returns both. The container must have compatible meta-annotations, at least as long a retention and the same or fewer targets, or the compiler rejects it.
6Follow-up questions
Why does @Inherited not work for annotations on interfaces?
Because a class can implement several interfaces, and they could carry the same annotation with different values, which would make the inherited value ambiguous. Java avoids the problem by only inheriting along the single superclass chain. Spring's AnnotationUtils.findAnnotation and MergedAnnotations with the TYPE_HIERARCHY search strategy do search interfaces, which is why Spring annotations placed on interfaces often still work.
Can a meta-annotation be our own annotation?
Yes. Any annotation whose @Target includes ANNOTATION_TYPE (or TYPE) can be placed on another annotation type. Java itself attaches no meaning to that, but frameworks like Spring do, which is how @Service carries @Component.
What happens if we forget @Retention?
The annotation gets CLASS retention, so it is written to the class file but invisible to reflection. A framework or aspect that checks for it at run time will never find it, which is a common bug with custom annotations.
7Conclusion
In this answer, we saw that meta-annotations are annotations placed on annotation type declarations. @Retention chooses SOURCE, CLASS (the default) or RUNTIME; @Target limits where the annotation may be used; @Documented shows it in the Javadoc of annotated elements; @Inherited lets a class-level annotation pass to subclasses through getAnnotation, but never from interfaces or onto methods, because multiple interfaces would make it ambiguous; and @Repeatable names a container so the annotation can appear several times, read back with getAnnotationsByType.
Open with
Meta-annotations are annotations we put on an annotation type declaration, so they describe the annotation itself rather than our code. There are five standard ones. @Retention decides how long the annotation lives: SOURCE is thrown away by the compiler, CLASS is written into the class file but not visible to reflection, and that is the default if you leave it out, and RUNTIME is visible through reflection, which is what frameworks need. @Target restricts where it may be placed, like TYPE, METHOD, FIELD, PARAMETER or TYPE_USE, so misuse becomes a compile error. @Documented makes the annotation show up in the Javadoc of the elements that carry it. @Inherited means that if the annotation is on a class, getAnnotation on a subclass also finds it.
Then, if they want more
It only works along the superclass chain: not from interfaces and not for methods, because a class can implement several interfaces with conflicting values, so Java avoids the ambiguity. Spring has its own utilities that do search interfaces.
And @Repeatable, since Java 8, lets the same annotation appear several times on one element; you name a container annotation holding an array, the compiler wraps the repeated uses in it, and you read them with getAnnotationsByType, because getAnnotation then returns null.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "What are meta-annotations? Explain @Retention, @Target, @Documented, @Inherited and...": https://scrivio-interview-library.netlify.app/java/annotations/595ba5ec41/