Java @Documented Annotations
Last Updated :
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 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");
}
}
OutputThis 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 annotation
import 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 class
public 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");
}
}
OutputThis 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
Similar Reads
Annotations in Java Annotations are used to provide supplemental information about a program. Annotations start with â@â.Annotations do not change the action of a compiled program.Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.A
9 min read
Inherited Annotations in Java Annotations in Java help associate metadata to the program elements such as classes, instance variables, methods, etc. Annotations can also be used to attach metadata to other annotations. These types of annotations are called meta-annotation. Java, by default, does not allow the custom annotations
5 min read
The @Deprecated Annotation in Java The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is. It's no longer relevant. It is so unimportant that you should stop using it because it has been su
5 min read
Hibernate - Annotations Annotation in JAVA is used to represent supplemental information. As you have seen @override, @inherited, etc are an example of annotations in general Java language. For deep dive please refer to Annotations in Java. In this article, we will discuss annotations referred to hibernate. So, the motive
7 min read
Scala | Annotation Scala Annotations are metadata added to the program source code. Annotations are allowed on any kind of definition or declaration including vals, vars, classes, objects, traits, defs and types. Annotations are used to associate meta-information with definitions. Syntax: @annot(exp_{1}, exp_{2}, ...)
3 min read
Customize Java Annotation with Examples Java annotations are a mechanism for adding metadata information to our source code (Program). They are a powerful part of Java that was added to JDK5. Annotations provide an alternative to the use of XML descriptors. Also, we are able to attach them to packages, classes, interfaces, methods, and fi
3 min read