The @Documented
annotation is a meta-annotation in Java—meaning it is used to annotate other annotations.Its primary purpose is to ensure that custom annotations appear in the generated JavaDoc documentation. By default, annotations are not included in JavaDocs unless they are marked with @Documented
.
Purpose
When you define a custom annotation and use @Documented
on it, tools like javadoc
will include that annotation in the API documentation of elements (classes, methods, etc.) annotated with it.
Syntax
@Documented
public @interface YourAnnotation {
// elements
}
Code language: PHP (php)
Simple Program (Basic Annotation with @Documented)
import java.lang.annotation.*; import java.lang.reflect.*; // Define a documented annotation @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @interface Developer { String author(); String date(); } // Use the annotation on a class @Developer(author = "LotusJavaPrince", date = "2025-05-25") class BankService { public void connect() { System.out.println("Connecting to bank database..."); } } public class DocumentedExample { public static void main(String[] args) { Class<BankService> obj = BankService.class; // Check if annotation is present if (obj.isAnnotationPresent(Developer.class)) { Developer dev = obj.getAnnotation(Developer.class); System.out.println("Author: " + dev.author()); System.out.println("Date: " + dev.date()); } } }
Author: LotusJavaPrince
Date: 2025-05-25
Code language: HTTP (http)
The @Documented
annotation plays a vital role in improving API transparency by making sure that custom annotations are visible in the generated documentation. This is especially important for collaborative projects or public libraries. When used wisely, it greatly enhances the maintainability and clarity of Java applications.