Custom annotation in java

So far we have discussed the fundamental idea of annotation.Please read my previous article for  basic idea about annotation.In simple annotations gives metadata about a program.Java itself has has some annotations inbuilt in it like @override.Also we can create  Custom annotation in java . Here we are discussing about how to create a  custom annotation in Java.We are explaining  a custom annotation example too.

Custom annotation in java

Now let us see how we can create a custom annotation to use in our application.We have a class , and that class contains few methods.Our main class needs to execute the methods based on a predefined  priority value.We are using custom defined annotation for setting the priority for each method.Integer value 1 is considered as maximum priority , 3 is considered as minimum and 2 is considered as medium.The annotation defined priority  is setting with each method.

step 1.Create an interface  for annotation.

Here we are creating an interface

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public @interface PriorityAnnotation {
int priority() default 3;

See the @interface in the definition.It indicates that the interface is using for defining an annotation.Also please note the @Retention just above the interface definition. The RetentionPolicy.RUNTIME  indicates that the annotation is a run time annotation.Inside the interface , please see the priority() deceleration. The default value is set as 3(lowest priority)

Step 2.Create a class that uses the above defined annotation

Now we need to create  a class with few methods and all the methods annotated with the above mentioned annotation.

public class PrioritySample {
public PrioritySample(){

@PriorityAnnotation(priority = 3)
public void displayOne(){
@PriorityAnnotation(priority = 1)
public void displayTwo(){
@PriorityAnnotation(priority = 2)
public void displayThree(){
@PriorityAnnotation(priority = 3)
public void displayFour(){

So our displayOne() and displayFour() methods are having the least priority.The displayThree() is having the medium priority.The displayTwo() is having the highest priority.

3)Use our annotation

Our main class should create an object of   PrioritySample using refection method(Reflection :-Object creation using Class.forName(“class name with full package”) ) . Then it should get all methods and annotations with those methods.Then the code filters all the methods based on priority and executing methods based on priority.

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

public class PriorityMain {
public PriorityMain() {
public void testAnnotation() {
try {
Method[] methods = Class.forName("com.annotation.PrioritySample").getMethods();
List firstMethods = new LinkedList();
List secondMethods = new LinkedList();
List thirdMethods = new LinkedList();
for (Method method : methods) {
PriorityAnnotation annotation = method.getAnnotation(PriorityAnnotation.class);
if (null != annotation) {
if (annotation.priority() == 1) {
} else if (annotation.priority() == 2) {
} else if (annotation.priority() == 3) {
PrioritySample sample = PrioritySample.class.newInstance();
for (Method firstMethod : firstMethods) {
for (Method secondMethod : secondMethods) {
for (Method thirdMethod : thirdMethods) {

} catch (ClassNotFoundException e) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
} catch (InstantiationException e) {
public static void main(String[] args) {
PriorityMain main = new PriorityMain();

Remember , while initializing the object using reflection I used com.annotation.PrioritySample because that is the class name with full package name in my work space.Now let us run our If our annotation is working at run time ,  then methods with priority value 1 should be executed first , then methods with priority 2 and finally methods with priority value 3.






So the displayTwo() is executing first and displayFour() is executing last.So our annotation is working.This sample application doesn’t make any practical sense.But it is useful to understand the concept.

Leave a Reply

Your email address will not be published. Required fields are marked *