Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Create Your Own Annotations in Java?

How to Create Your Own Annotations in Java?

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:

  1. Predefined annotations.: @Deprecated, @Override, @SuppressWarnings, @SafeVarargs, @FunctionalInterface.
  2. Meta-annotations: @Retention, @Documented, @Target, @Inherited, @Repeatable.
  3. 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:

  1. To create your own Java Annotation you must use @interface Annotation_name, this will create a new Java Annotation for you.
  2. The @interface will describe the new annotation type declaration.
  3. 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:  

  1. Marker Annotations: These are the annotations inside which no variables are declared or defined.
  2. Single-value Annotations: These are the annotations inside which only a single variable is declared or defined.
  3. 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.");
    }
}


Output

example 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.");
    }
}


Output

example 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.");
    }
}


Output

example 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");
    }
}


Output

how 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");
    }
}


Output

Annotation 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.

 

RELATED ARTICLES

Most Popular

Recent Comments