Annotations Types
In this tutorial, we are going to discuss about Annotations Types in java. Annotations are declared by using following syntax
@interface AnnotationName {
members
}
Annotation types are defined using the @interface
keyword, and they can include elements that represent values associated with the annotation.
Members syntax
datatype memberName() [default value]
Annotations Utilization Syntax
In java annotations are applicable for variables, methods, constructors, interfaces, another annotation, enum and classes.
Syntax
@AnnotationName (member1 = value1, member2 = value2,…...)
Based on annotation members, annotations are divided in to 3 types.
- Marker Annotation
- Single valued Annotation
- Multi valued Annotation
1. Marker Annotation
A marker annotation is a special type of annotation which contains no members.
E.g:
@Override
2. Single valued Annotation
Single valued annotation is the annotation which contains only one member.
E.g:
@SupressWarnings("unchecked")
3. Multi valued Annotation
Multi valued annotation is the annotation which contains more than one member.
E.g.
@WebServlet(
name = "MyServlet",
description = "This is my first annotated servlet",
urlPatterns = "/processServlet"
)
Annotation types are a powerful mechanism in Java for associating metadata with program elements, enabling a wide range of use cases such as documentation, validation, configuration, and code generation.
That’s all about the Annotations Types in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!