Java Annotation Targets

Annotation targets restrict which kinds of Java program elements may legally receive an annotation. They are declared with `@Target` and `ElementType` values such as `METHOD`, `FIELD`, or `TYPE`.

Java annotations can be limited to specific program elements with the `@Target` meta-annotation. For example, `@Target(ElementType.METHOD)` means the annotation may be placed on methods, but not on classes or fields.

`ElementType` provides the allowed target categories. Common values include `TYPE` for classes, interfaces, enums, and annotation types; `METHOD` for methods; `FIELD` for fields; `PARAMETER` for parameters; and `CONSTRUCTOR` for constructors. Other targets include `LOCAL_VARIABLE`, `PACKAGE`, `MODULE`, `TYPE_USE`, and `TYPE_PARAMETER`.

If an annotation declares multiple target types, place them in an array, such as `@Target({ElementType.TYPE, ElementType.METHOD})`. The compiler then checks annotation placement and reports an error when the annotation is used on an element outside its declared targets.

If an annotation has no `@Target`, Java permits it to be used in the contexts allowed by the annotation rules rather than imposing an explicit target restriction. `@Target` therefore communicates and enforces the intended usage boundary of a custom annotation.

Key points

Interview questions

Does `@Target` determine whether reflection can see an annotation?

No. `@Target` and `@Retention` solve different problems. `@Target` controls legal placement, while `@Retention` determines how long the annotation is retained. For reflection at runtime, an annotation generally needs `RetentionPolicy.RUNTIME`.

What is the difference between `TYPE` and `TYPE_USE`?

`TYPE` targets declarations of types, such as classes, interfaces, enums, and annotation types. `TYPE_USE` targets uses of a type rather than only its declaration. This allows annotations in locations such as generic type arguments and other type-use contexts. The distinction matters when designing annotations for type-related tooling such as static analysis.

What is `@Target` used for in Java annotations?

`@Target` specifies the program elements on which an annotation is permitted. It takes one or more `ElementType` values. For example, `@Target(ElementType.METHOD)` restricts an annotation to methods. The compiler checks this restriction when the annotation is used.

How do you allow an annotation on both classes and methods?

Declare `@Target` with an array containing both target types. The declaration would use `@Target({ElementType.TYPE, ElementType.METHOD})`. This permits the annotation on type declarations and method declarations. Other locations remain disallowed unless they are also listed.

Why would you restrict a custom annotation instead of leaving it unrestricted?

A target restriction makes the annotation's contract explicit and prevents accidental misuse. It also gives the compiler an opportunity to catch incorrect placement early. For framework or infrastructure annotations, this can reduce ambiguity about how processors, reflection code, or tooling should interpret the annotation. The appropriate targets should match the annotation's actual semantic purpose.