Java @Documented Annotations - GeeksforGeeks

07 Aug, 2021
By default, Java annotations are not shown in the documentation created using the Javadoc tool. To ensure that our custom annotations are shown in the documentation, we use @Documented annotation to annotate our custom annotations. @Documented is a meta-annotation (an annotation applied to other annotations) provided in java.lang.annotation package.
Cases:
Without using @Documented annotationUsing @Documented annotation
Without using @Documented annotation
Using @Documented annotation
Let us discuss both the scenario to a certain depth. 
Case 1: Without using @Documented annotation
In the code example shown below, we have created a custom annotation named CustomAnnotation. After that, we have annotated our class named DocumentedAnnotationDemo with it. Finally, we created documentation using the Javadoc tool. The syntax to use the Javadoc utility on the command prompt is mentioned below.
javadoc NameOfClassFile.java
Example
Java
// Java Program to Illustrate Documented Annotations// Without using @Documented annotation // Creating a single value custom annotation@interface CustomAnnotation {    String value();} @CustomAnnotation("GFG")public class DocumentedAnnotationDemo {    public static void main(String[] args) {        System.out.println("This is the main method");    }}
This is the main method
When we create documentation of the code example shown above, the custom annotation used to annotate our DocumentedAnnotationDemo class is not shown in the documentation and depicted in the snapshot shown below.
Documentation without using @Documented annotation
Case 2: Using @Documented annotation
In the code example shown below, we have again created the same custom annotation named CustomAnnotation, but in this case, we used @Documented to annotate our custom annotation. After that, we have annotated our class named DocumentedAnnotationDemo with it. Finally, we created documentation using the Javadoc tool. 
Example
Java
// Java Program to Illustrate Documented Annotations// With using @Documented annotation // Importing the Documented annotationimport java.lang.annotation.Documented; // Creating a single value custom annotation// which is annotated using @Documented// annotation@Documented @interface CustomAnnotation { String value(); } // This annotation will be documented@CustomAnnotation("GFG") // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {         // Print and display statement on the console        System.out.println("This is the main function");    }}
This is the main function
When we now create documentation of the code example shown above, the custom annotation used to annotate our DocumentedAnnotationDemo class is shown in the documentation due to the use of @Documented annotation while creating it. A snapshot of the documentation created in this case is shown below.
Documentation using @Documented annotation
 
simranarora5sos
gulshankumarar231
Java-Annotation
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
Difference between Abstract Class and Interface in Java
How to remove an element from ArrayList in Java?