How to Create Your Own Annotations in Java?
Last Updated :
17 Aug, 2022
Annotations are a form of metadata that provide information about the program but are not a part of the program itself. An Annotation does not affect the operation of the code they Annotate.
Now let us go through different types of java annotations present that are listed as follows:
- Predefined annotations.: @Deprecated, @Override, @SuppressWarnings, @SafeVarargs, @FunctionalInterface.
- Meta-annotations: @Retention, @Documented, @Target, @Inherited, @Repeatable.
- Custom annotations: These are defined by the user. (We will be learning to create custom annotations in this module).
Geek, now you must be wondering how can we create our own java annotations, for that refer to simple steps sequentially as follows:
- To create your own Java Annotation you must use @interface Annotation_name, this will create a new Java Annotation for you.
- The @interface will describe the new annotation type declaration.
- After giving a name to your Annotation, you will need to create a block of statements inside which you may declare some variables.
Now proceeding there are three forms of annotations that can be defined in java as follows:
- Marker Annotations: These are the annotations inside which no variables are declared or defined.
- Single-value Annotations: These are the annotations inside which only a single variable is declared or defined.
- Multi-value Annotations: These are the annotations inside which multiple variables of multiple types can be declared and defined.
Implementation:
Let us take an example of a custom annotation called books_data to understand how different forms of annotations are declared.
@interface books_data //using the syntax : @interface Annotation_name, we declared a new annotation here.
{ //beginning of annotation declaration and definition
/*
defining variables inside an annotation is optional.
The number of variables declared inside an annotation will describe its form.
*/
} //end of annotation declaration and definition
Example 1:
Java
// Java Programwhere Illustrating Declaration of
// Custom Marker Annotation
// Importing I/O classes
import java.io.*;
// Sample for marker Annotation:
// Custom annotation declaration
@interface books_data
{
// No variable declared here
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of Marker Annotations.");
}
}
Outputexample of Marker Annotations.
Since no variable is declared inside this annotation, this will be known as Marker Annotation.
Example 2:
Java
// Java Program Illustrating Declaration of
// Custom Single Value Annotation
// Importing input output classes
import java.io.*;
// Sample for single value Annotation:
// Custom annotation declaration
@interface books_data
{
// Single variable declaration
String book_name();
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of single value Annotation.");
}
}
Outputexample of single value Annotation.
We can see that we have declared a variable book_name of type String, and it's the only variable declared inside the Annotation, hence, this is an example of Single-Value Annotation.
Example 3: We will declare the following variable inside our annotation:
@interface books_data {
String book_name();
int book_price();
String author();
}
Java
// Java Programwhere Illustrating Declaration of
// Multi value Annotation
// Importing input output classes
import java.io.*;
// Sample for multi value annotation:
// Custom annotation declaration
@interface books_data
{
// Multiple variable declarations
String book_name();
int book_price();
String author();
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of multi value Annotation.");
}
}
Outputexample of multi value Annotation.
We have declared multiple variables inside our Annotation, this is an example of Multi-value Annotation.
Now let us see how to use custom annotations for which let us look at how you can use your custom annotation:
- Method 1: Default annotations
- Method 2: Custom annotations
Focusing onto custom annotations for which in order to use your custom annotation, we simply need to call your annotation using your annotation Name preceded with @symbol and pass the value of the declared variables in an ordered manner to the variables that you have declared in the annotation.
Example 1:
Java
// Java Program illustrating Use of Custom Annotation
// Importing input output classes
import java.io.*;
// Sample for marker annotation:
// Custom annotation declaration
@interface books_data
{
// Multiple variable declaration
String book_name();
int book_price();
String author();
}
// Using the custom Annotation
@books_data(book_name = "Effective Java", book_price = 30,
author = "Joshua Bloch")
// Class 1
class book_store {
}
// Class 2
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println("how to use the annotations");
}
}
Outputhow to use the annotations
However, if you don't want to specify values when you are using the Annotation you can initialize the values inside your Annotation using the default value.
So finally let us discuss how it is done
Note: Using default value is optional.
Example 2:
Java
// Java Program Illustrating Default Values Declaration
// of Variables Inside an Annotation
// Importing input output classes
import java.io.*;
// Sample for Marker annotation
@interface books_data
{
// Custom annotation declaration
String book_name() default "Effective Java";
// Declaring the default values
int book_price() default 30;
String author() default "Joshua Bloch";
// Multiple variable declaration
}
// Using the custom Annotation
@books_data
// Class 1
class book_store {
}
// Class 2
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"Annotation using default values");
}
}
OutputAnnotation using default values
If you still initialize the values when you are using your annotation when you have declared the default values, the initialized value will overwrite the default values.
Similar Reads
Java | How to create your own Helper Class?
In Java, a Helper class is a class that contains useful methods which make common tasks easier, like error handling, checking input, etc. This class intends to give a quick implementation of basic functions so that the programmers do not have to implement them again and again. This class is easy to
7 min read
The @Deprecated Annotation in Java
The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is. It's no longer relevant. It is so unimportant that you should stop using it because it has been su
5 min read
How to add Text Annotations in Seaborn
You've used vibrant crayons to make an exquisite painting. Wouldn't it be interesting to add some text now that the picture meaning is clear? With Seaborn, we're going to accomplish just that ! Seaborn is similar to a magical instrument that enables us to use data to produce incredible graphs or vis
5 min read
How to Capture Data using @RequestParam Annotation in Spring?
The @RequestParam annotation enables Spring to capture input data that may be passed as a query, form data, or any arbitrary custom data. It is used to bind a web request parameter to a method parameter. Here, we are going to understand these two above lines, and we will see how we can capture data
6 min read
How to Create Custom Class in Java?
Class is the collection of objects. Class is not a real-world entity it is just only templates and prototypes or blueprints. Class does not occupy memory. We can write a custom class as per our choice for an illustration purpose a sample is shown in the program below as a helper class. Example: Java
2 min read
Annotations in Java
Annotations are used to provide supplemental information about a program. Annotations start with â@â.Annotations do not change the action of a compiled program.Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.A
9 min read
How to create a user defined javap tool?
What is a javap tool? The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used ("-c" or "-verbose" for byte code and byte code along with innards info,
4 min read
Java @Documented Annotations
By default, Java annotations are not shown in the documentation created using the Javadoc tool. To ensure that our custom annotations are shown in the documentation, we use @Documented annotation to annotate our custom annotations. @Documented is a meta-annotation (an annotation applied to other ann
2 min read
How to Improve Your Android Coding Through Annotation?
Annotations are a type of metadata. Metadata, on the other hand, is a collection of data that provides information about other data. There are numerous ways to use annotations in Android. However, in this section, we will discuss how annotations may be utilized to better our Android coding. Official
3 min read
How to Call a Method in Java?
In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.What is a Metho
3 min read