Quick revision
The short answer
TYPE_PARAMETER allows an annotation on the declaration of a generic type parameter, such as <@NonNull T>; TYPE_USE allows it on any use of a type, including casts, generic arguments, array types, throws clauses and new expressions; MODULE allows it on a module declaration in module-info.java; and RECORD_COMPONENT allows it on a component in a record header. When an annotation on a record component has several targets, the compiler propagates it to every implicit element its targets allow: the private field for FIELD, the accessor for METHOD, the canonical constructor parameter for PARAMETER, the component itself for RECORD_COMPONENT, and the types for TYPE_USE.
What to remember
In this answer, we went through four newer targets. TYPE_PARAMETER covers declarations of generic type parameters, TYPE_USE covers any use of a type, including generic arguments, arrays, casts and throws clauses, MODULE covers module declarations, and RECORD_COMPONENT covers components in a record header. An annotation on a record component is propagated to each implicit element its targets allow: the field, the accessor, the canonical constructor parameter, the component and the types. To place an annotation only on the accessor, we restrict its target to METHOD or declare the accessor explicitly.
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.
TYPE_PARAMETER allows an annotation on the declaration of a generic type parameter, such as <@NonNull T>; TYPE_USE allows it on any use of a type, including casts, generic arguments, array types, throws clauses and new expressions; MODULE allows it on a module declaration in module-info.java; and RECORD_COMPONENT allows it on a component in a record header. When an annotation on a record component has several targets, the compiler propagates it to every implicit element its targets allow: the private field for FIELD, the accessor for METHOD, the canonical constructor parameter for PARAMETER, the component itself for RECORD_COMPONENT, and the types for TYPE_USE.
Background to this question
Most developers know the classic targets, TYPE, METHOD and FIELD. The newer ones came with generics-aware tooling in Java 8, modules in Java 9 and records in Java 16, and they matter in real code because validation, nullness checking and serialisation libraries rely on them. Records are the tricky case: one annotation in the header can end up in four places, and whether a framework sees it depends on where it landed. In this answer, we will go through each target with an example, then follow an annotation on a record component to see exactly where it propagates, and finish with how to make an annotation apply only to the accessor.
1TYPE_PARAMETER: Annotating a Generic Parameter's Declaration
A type parameter is the T in class Box<T> or in <T> T first(List<T> list). TYPE_PARAMETER lets us annotate that declaration, saying something about every type that may be substituted for T. Checker frameworks use this, for example to declare that a container's elements may never be null.
Notice this is the declaration only. Using T later, as in List<T>, is a type use, which needs TYPE_USE.
2TYPE_USE: Annotating Any Use of a Type
TYPE_USE, added in Java 8, is much broader. It allows an annotation anywhere a type is written: in a field or variable type, a generic argument, an array component, a cast, a new expression, an implements clause or a throws clause. Since Java 8, TYPE_USE also covers the places TYPE_PARAMETER covers. This is what pluggable type checkers such as the Checker Framework or NullAway build on, and why JSpecify's @Nullable targets TYPE_USE.
Notice the array example. In String @Nullable [], the annotation applies to the array, not to its String elements; @Nullable String [] would mean nullable elements. TYPE_USE annotations attach to the exact type they precede, which gives precision but takes some getting used to.
3MODULE and RECORD_COMPONENT
MODULE allows annotations on a module declaration in module-info.java. The most common one is @Deprecated on a whole module; tools can define their own, for example to mark a module as internal.
RECORD_COMPONENT, from Java 16, is the target for annotations that describe the component itself and are read through RecordComponent from Class.getRecordComponents(). A JSON serialiser could use @Sensitive to mask the tax id.
4How Annotations on Record Components Propagate
A record header such as record Customer(String name, @NotBlank String taxId) is short for a lot of generated code: a private final field taxId, an accessor method taxId(), and a canonical constructor with a taxId parameter. The annotation we write once in the header could belong on any of these. Java's rule is propagation by target: the compiler copies the annotation to each implicitly declared element whose kind is allowed by the annotation's @Target, and silently skips the others. If no target applies at all, it is a compile error.
| Annotation @Target includes | Copied to | Read through |
|---|---|---|
| FIELD | The private final field | Field.getAnnotation |
| METHOD | The accessor taxId() | Method.getAnnotation |
| PARAMETER | Canonical constructor parameter | Parameter.getAnnotation |
RECORD_COMPONENT | The component itself | RecordComponent.getAnnotation |
TYPE_USE | The type at each of those places | getAnnotatedType() |
So one annotation can land in three or four places. That is usually what we want, but it can surprise frameworks that read the annotation in more than one place, for example a validator that checks both the field and the getter and reports the same violation twice. Hibernate Validator handles records sensibly, but it is worth checking with our own annotations and processors.
5Making an Annotation Apply Only to the Accessor
Sometimes we want the annotation in one place only. There are two ways. If we own the annotation, we give it @Target(ElementType.METHOD) alone; propagation then puts it on the accessor and nowhere else. If we do not own it and its targets are broad, like @NotBlank, we leave the header clean and declare the accessor explicitly, annotating it there.
Explicitly declared members are not affected by propagation; only implicit ones are. Notice that @Override is allowed on an explicit record accessor, and it makes the compiler check that the method really matches a component.
6Follow-up questions
How would you make a validation annotation apply only to the accessor of a record?
Declare the accessor explicitly in the record body and put the annotation there, leaving the component in the header unannotated. Propagation only affects implicitly declared members, so the field and constructor parameter stay clean. If we own the annotation, giving it @Target(ElementType.METHOD) alone achieves the same while still writing it in the header.
Why are TYPE_USE annotations preferred for nullness?
Because nullness is a property of a type, not of a declaration. With TYPE_USE we can say List<@Nullable String> versus @Nullable List<String>, which a declaration annotation on a field cannot express. That precision is what static checkers need to reason about generics.
Can a TYPE_USE annotation be read at run time?
Yes, if it has RUNTIME retention, through the AnnotatedType API: for example field.getAnnotatedType() and then getAnnotatedActualTypeArguments() for generic arguments. Annotations in casts and local variable types are recorded in the class file but are not exposed through core reflection.
7Conclusion
In this answer, we went through four newer targets. TYPE_PARAMETER covers declarations of generic type parameters, TYPE_USE covers any use of a type, including generic arguments, arrays, casts and throws clauses, MODULE covers module declarations, and RECORD_COMPONENT covers components in a record header. An annotation on a record component is propagated to each implicit element its targets allow: the field, the accessor, the canonical constructor parameter, the component and the types. To place an annotation only on the accessor, we restrict its target to METHOD or declare the accessor explicitly.
Open with
TYPE_PARAMETER lets you annotate the declaration of a generic type parameter, like class Box<@NonNull T>. TYPE_USE, since Java 8, is much broader: it allows an annotation on any use of a type, so generic arguments like List<@Nullable String>, array types, casts, new expressions, implements and throws clauses.
Then, if they want more
That is what nullness checkers and JSpecify build on, because nullness is a property of a type. MODULE allows annotations on a module declaration in module-info.java, for example @Deprecated on a whole module. RECORD_COMPONENT, since Java 16, allows annotations on a component in a record header, and you read them through Class.getRecordComponents. The interesting part is records.
A component like @NotBlank String taxId generates a private field, an accessor, and a canonical constructor parameter, and the compiler propagates the annotation to every one of those whose kind is in the annotation's @Target, and skips the others.
So @NotBlank, which targets FIELD, METHOD, PARAMETER and TYPE_USE, ends up on the field, the accessor, the constructor parameter and the types. If no target fits at all, it is a compile error.
If I want it only on the accessor, I either restrict the annotation's target to METHOD, if I own it, or I leave the header clean and declare the accessor explicitly with the annotation on it, since explicit members are not affected by propagation.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 16, Java 9
- 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 do ElementType.TYPE_PARAMETER, TYPE_USE, MODULE and RECORD_COMPONENT targets allow...": https://scrivio-interview-library.netlify.app/java/annotations/c745399659/