Fundamentals 5 min read

Understanding Java Meta-Annotations: @Target, @Retention, @Documented, and @Inherited

This article explains Java meta‑annotations—@Target, @Retention, @Documented, and @Inherited—detailing their purposes, possible values, and providing code examples to show how they control annotation scope, lifecycle, documentation inclusion, and inheritance in Java programs.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Understanding Java Meta-Annotations: @Target, @Retention, @Documented, and @Inherited

Java meta‑annotations are annotations that annotate other annotations, allowing developers to define an annotation's retention policy, applicable targets, documentation inclusion, and inheritance behavior.

The four core meta‑annotations are @Retention , @Target , @Documented , and @Inherited , each serving a specific purpose.

@Target

@Target specifies where an annotation can be applied, with possible ElementType values such as TYPE , FIELD , METHOD , PARAMETER , CONSTRUCTOR , LOCAL_VARIABLE , ANNOTATION_TYPE , and PACKAGE .

Example:

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface MyAnnotation {
    // ...
}

In this example, @Target allows MyAnnotation to be used on classes, fields, and methods.

@Retention

@Retention defines how long the annotation is retained: SOURCE , CLASS , or RUNTIME .

Example:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    // ...
}

Here the annotation is retained at runtime and can be accessed via reflection.

@Documented

@Documented indicates that the annotation should appear in generated Javadoc.

Example:

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    // ...
}

This makes the annotation visible in Javadoc.

@Inherited

@Inherited allows the annotation to be inherited by subclasses.

Example:

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    // ...
}

Subclasses of a class annotated with @MyAnnotation will inherit the annotation.

For further study, the author offers a 300,000‑word collection of Alibaba architecture materials and a comprehensive Java interview question set, available through the public account “mikechen的互联网架构”.

backendJavaProgrammingAnnotationsMeta‑annotations
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.