Course contentsShow
← Java
Lesson 2449 of 2,87046. AnnotationsFree lesson

ElementType.ANNOTATION_TYPE: Meta-Annotations

Create meta-annotations that can be applied to other annotation declarations.

ElementType.ANNOTATION_TYPE: Meta-Annotations

What you'll learn: How to create annotations that can only be applied to other annotations themselves.

What Are Meta-Annotations?

A meta-annotation is an annotation that annotates other annotations. Think of it like a "label for labels" — just as you annotate classes or methods to add metadata, you can annotate annotation declarations to control how those annotations behave.

You've already seen meta-annotations in action: @Retention and @Target themselves are meta-annotations! They're applied to annotation declarations, not to regular classes or methods.

Using ElementType.ANNOTATION_TYPE

When you want to restrict an annotation so it can only be applied to annotation declarations, use ElementType.ANNOTATION_TYPE in the @Target meta-annotation:

import java.lang.annotation.*;

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DocumentationInfo {
    String author();
    String version();
}

Now @DocumentationInfo can only be applied to other annotations:

@DocumentationInfo(author = "Jane Doe", version = "1.0")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyCustomAnnotation {
    String value();
}

Why Create Meta-Annotations?

Meta-annotations let you add metadata about your annotations — documenting who created them, tracking versions, marking them as experimental, or controlling framework behavior. They help you build annotation hierarchies and enforce constraints on how your custom annotations are used.

Analogy

Think of annotations as stickers you put on things. Meta-annotations are like "sticker specifications" — instructions about what the stickers themselves mean, who made them, or where they're allowed to be placed.

Key Takeaway: ElementType.ANNOTATION_TYPE restricts your annotation to only annotate other annotation declarations, creating meta-annotations that add metadata about annotations themselves.