How to Create Your Own Annotations in Java?


When we start learning Java, we often wonder about symbols like @override and @inherited. They are a special kind of tag termed as Annotations that can be applied to classes, methods, fields, parameters, and other elements of the code. Java provides support for some built-in annotations, however, we are allowed to create our own annotations too. In this article, we are going learn how to create and use our own custom annotations.

Creating Custom Annotations in Java

Before creating our own annotations, let's familiarize ourselves with the basics of annotations in Java.

Annotations

They are a powerful feature of Java that allows us to add metadata to our code. Here, the term metadata means additional information about a particular block of code. They can be used for various purposes including documentation, validation, testing, dependency injection and many more. They are preceded by the '@' symbol and can have optional attributes that provide additional information.

Java supports two types of annotations: built-in and custom. Built-in annotations have a predefined meaning and behavior.

Here are a few built-in annotations:

  • @Override: It indicates that a method overrides another method from a superclass or an interface.

  • @Deprecated: It is used to mark an element as obsolete so that it can generate a warning when it is used.

  • @SuppressWarnings: suppress the compiler warnings.

Till this point, we have introduced annotations and some of its predefined annotations. Now, let's discuss how we can create custom annotations.

To create our own Annotations

The first step is to declare it with the help of @interface keyword followed by the name of annotation. Then, the next step is to add some attributes that describe the newly created annotation. Attributes could be some variables.

Syntax

@interface nameOfAnnotation { // declaration
   // Attributes
}
@nameOfAnnotation( values ) // initialization

Example 1

In the following example, we will create an annotation with the name 'Author_details' along with its two mandatory attributes 'name' and 'email'.

// declaring the annotation
@interface Author_details {
   // attributes of annotation
   String name(); 
   String email(); 
}
// to use the annotation
@Author_details(name = "Shriansh Kumar", email = 
"shriansh.kumar@tp.com")
public class Example1 {
   public static void main(String[] args) {
      System.out.println("This is an example of Annotation");
   }
}

Output

This is an example of Annotation

Example 2

In the following example, we will declare an annotation with the name 'Author_details' along with its two default attributes 'name' and 'email'. To define a default attribute, we need to use 'default' keyword.

// declaring the annotation
@interface Author_details {
   // attributes of annotation
   String name() default "Shriansh Kumar"; 
   String email() default "shriansh.kumar@tp.com"; 
}
// to use the annotation
@Author_details
public class Example2 {
   public static void main(String[] args) {
      System.out.println("This is an example of Annotation");
   }
}

Output

This is an example of Annotation

Example 3

In the following example, we will create and use a single value annotation.

// declaring the annotation
@interface Author_details {
   // attributes of annotation
   String name() default "Shriansh Kumar"; 
}
// to use the annotation
@Author_details
public class Example3 {
   public static void main(String[] args) {
      System.out.println("This is an example of Annotation");
   }
}

Output

This is an example of Annotation

Conclusion

An annotation just tells us the information about the block of code, it does not affect the working of whole code. In this article, we first learned built-in annotations and in the later sections, we discussed the process of creating and using custom annotations with the help of examples.

Updated on: 19-Jul-2023

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements