Java custom annotation:
We can also create our own java annotations. Like a Java class or interface, java annotations are also defined in their own file. Java custom annotations are created by using @interface, followed by annotation name. An annotation can have elements which looks like methods. Annotation elements can have default values. For those elements values can be skipped while using annotation.
Syntax:
@interface CustomAnnotationName{} |
@interface CustomAnnotationName{}
Types of Custom Java Annotation
- Marker Annotation
- Single-Value Annotation
- Multi-Value Annotation
Marker Annotation: An annotation with no element. The @Override and @Deprecated are marker annotations.
Example:
@interface CustomAnnotation{} |
@interface CustomAnnotation{}
Single-Value Annotation: An annotation with single element. Element can have default value.
Example:
@interface CustomAnnotation {
int value();
} |
@interface CustomAnnotation {
int value();
}
Example with default value:
@interface CustomAnnotation {
int value() default 0;
} |
@interface CustomAnnotation {
int value() default 0;
}
Multi-Value Annotation: An annotation with multiple elements. Elements can have default values.
Example:
@interface CustomAnnotation {
int value1();
String value2();
} |
@interface CustomAnnotation {
int value1();
String value2();
}
Java Built-in Annotations used in custom annotations:
- @Target
- @Retention
- @Inherited
- @Documented
@Target annotation
The @Target annotation is used to specify where we can use the annotation. The ElementType enum contains the following possible targets for a java annotation:
ElementType.METHOD
ElementType.PACKAGE
ElementType.PARAMETER
ElementType.TYPE
ElementType.ANNOTATION_TYPE
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.FIELD
Note: If we no Target type is defined for an annotation that means annotation can be applied to any element.
Example to create annotation for a class
@Target(ElementType.TYPE)
@interface CustomAnnotation{
int value1();
String value2();
} |
@Target(ElementType.TYPE)
@interface CustomAnnotation{
int value1();
String value2();
}
Example to create annotation for a class or methods
@Target({ElementType.TYPE, ElementType.METHOD})
@interface CustomAnnotation{
int value1();
String value2();
} |
@Target({ElementType.TYPE, ElementType.METHOD})
@interface CustomAnnotation{
int value1();
String value2();
}
@Retention annotation:
The @Retention annotation is used to specify how long annotations with the annotated type will be available. RetentionPolicy provides following types of retentions:
- RetentionPolicy.RUNTIME: The annotation will be available at runtime. It will be available to java compiler and JVM.
- RetentionPolicy.CLASS: The annotation would be in the .class file but it would not be available at runtime. i.e. available to java compiler but not to JVM.
- RetentionPolicy.SOURCE: The annotation would be available in the source code. It would neither be available to java compiler nor to JVM.
Example:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation{
int value1();
String value2();
} |
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation{
int value1();
String value2();
}
@Inherited annotation:
The @Inherited annotation indicates that a custom Java annotation used in a class should be inherited by all its subclasses.
In the below example the ParentClass class is using annotation @CustomAnnotation which is marked with @inherited annotation. So the sub class ChildClass inherits the @CustomAnnotation because ChildClass inherits from ParentClass.
Example:
java.lang.annotation.Inherited
@Inherited
public @interface CustomAnnotation {
}
@CustomAnnotation
public class ParentClass {
...
}
public class ChildClass extends ParentClass {
...
} |
java.lang.annotation.Inherited @Inherited
public @interface CustomAnnotation { } @CustomAnnotation
public class ParentClass {
...
} public class ChildClass extends ParentClass {
...
}
@Documented annotation:
The @Documented annotation is used to indicate the JavaDoc tool that custom annotation should be visible in the JavaDoc for classes using custom annotation.
Example:
import java.lang.annotation.Documented;
@Documented
public @interface CustomAnnotation {
}
@CustomAnnotation
public class TestClass { ... } |
import java.lang.annotation.Documented; @Documented
public @interface CustomAnnotation { } @CustomAnnotation
public class TestClass { ... }
Download this example.
Java custom annotation example
package com.tutorialspointexamples;
//Creating annotation
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation{
int value();
}
//Using annotation
class Hello{
@MyAnnotation(value=50)
public void sayHello(){
System.out.println("Hello java annotations");
}
}
//Accessing annotation
public class CustomAnnotationTest {
public static void main(String args[]) throws Exception{
Hello hello=new Hello();
Method method=hello.getClass().getMethod("sayHello");
MyAnnotation manno=method.getAnnotation(MyAnnotation.class);
System.out.println("Value is: "+manno.value());
}
} |
package com.tutorialspointexamples; //Creating annotation
import java.lang.annotation.*;
import java.lang.reflect.*; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation{
int value();
} //Using annotation
class Hello{
@MyAnnotation(value=50)
public void sayHello(){
System.out.println("Hello java annotations");
}
} //Accessing annotation
public class CustomAnnotationTest {
public static void main(String args[]) throws Exception{
Hello hello=new Hello();
Method method=hello.getClass().getMethod("sayHello");
MyAnnotation manno=method.getAnnotation(MyAnnotation.class);
System.out.println("Value is: "+manno.value());
}
}
Output:
Download this example.