Types of Annotation
There are three types of annotations.
- Marker Annotation
- Single-Value Annotation
- Multi-Value Annotation
1) Marker Annotation
Marker annotation is an annotation without a method. For instance:
@interface MyAnnotation{}
The marker annotations @Override and @Deprecated are used.
2) Single-Value Annotation
Single-value annotation is a type of annotation that only contains one method. For instance:
@interface MyAnnotation{
int value();
}
We can also offer the default setting. For instance:
@interface MyAnnotation{
int value() default 0;
}
Application of Single-Value Annotation
Let’s look at the code for using the single value annotation.
@MyAnnotation(value=10)
The value can be anything.
3) Multi-Value Annotation
An annotation with multiple methods is known as a Multi-Value annotation. As an example:
@interface MyAnnotaion{
int value1();
String value1();
String value2();
String value3();
}
}
We can also provide the default value. As an example:
@interface MyAnnotation{
int value1() default 1;
String value2() default "";
String value3() default "xyz";
}
How to apply Multi-Value Annotation
Let’s look at the code for using the multi-value annotation.
@MyAnnotation(value1 = 10, value2 = "Arun Kumar", value3 = "Ghaziabad")
Built-in Annotations used in custom annotations in java
- @Target
- @Retention
- @Inherited
- @Documented
@Target
The @Target tag is used to specify which type the annotation is applied to.
The annotation in java.lang.
The ElementType enum declares a number of constants that specify the type of element to which annotation will be applied, such as TYPE, METHOD, FIELD, and so on. Let’s look at the ElementType enum constants:
Example to specify annotations for a class
@Target(ElementType.TYPE)
@interface MyAnnotation{
int value1();
String value2();
}
Example to specify annotations for a class, methods or fields
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@interface MyAnnotation{
int value1();
String value2();
}
@Retention
The @Retention annotation specifies the level of annotation that will be available.
Example to specify the RetentionPolicy
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation{
int value1();
String value2();
}
Example of custom annotation: creating, applying and accessing annotation
Let’s take a look at a basic example of creating, applying, and accessing annotation.
//Creating annotation
import java.lang.annotation.*;
import java.lang.reflect.*;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation{
int value(();
}//Applying annotation
class Hello{
@MyAnnotation(value = 10)public void sayHello(){System.out.println("hello annotation");}
}//Accessing annotation
class TestCustomAnnotation1{
public static void main(String args[])throws Exception{Hello h = new Hello();
Method m = h.getClass().getMethod("sayHello");MyAnnotation manno = m.getAnnotation(MyAnnotation.class);
System.out.pirntln("value is:" + manno.value());
}}
Output :
value is: 10
How built-in annotations are used in real scenarios?
In practice, a java programmer only needs to use annotation. He or she is not required to create or access annotations. The implementation provider is in charge of creating and accessing annotations. The java compiler or JVM performs some additional operations on behalf of the annotation.
@Inherited
Annotations are not inherited by subclasses by default. The @Inherited annotation indicates that the annotation will be inherited by subclasses.
@Inherited
@interface ForEveryone{}//Now it will be available to subclass also @interface ForEveryone{}
class Superclass{}class Subclass extends Superclass{}
@Documented
The @Documented Marks the annotation for inclusion in the documentation.