Java @Target Annotations


When we start learning Java, we often wonder about symbols like @override and @inherited written within the code blocks. 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. The @Target annotation is one of the types of meta-annotations that specifies the defined annotation type applicable to which code block element. Don't get confused by these terms, we will clear all the doubts and confusion as we move forward in this article.

The @Target Annotation of Java

The first thing we need to understand is what are annotations and what purpose they serve 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 learned about 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 

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

How to Annotate an Annotation Using @Target

Sometimes it may happen that the annotation created by a user might not convey its whole purpose. For these kinds of situations, Java provides four meta-annotations namely @Documentation, @Target, @Retention, and @Inherited to supply another metadata in the form of annotations to that user-defined annotation. It will help the compiler to enforce the required features during compilation.

The @Target annotation takes an array of values from the java.lang.annotation.ElementType enum as its parameter. The ElementType enum defines the possible kinds of elements that can be annotated in Java. The values are −

  • ANNOTATION_TYPE − An annotation type declaration

  • CONSTRUCTOR − A constructor declaration

  • FIELD − A field declaration

  • LOCAL_VARIABLE − A local variable declaration

  • METHOD − A method declaration

  • PACKAGE − A package declaration

  • PARAMETER − A parameter declaration

  • TYPE − A class, interface, enum, or record declaration

  • TYPE_PARAMETER − A type parameter declaration

  • TYPE_USE − A use of a type

Syntax

@Target({ElementType.nameOfanyValues})
@interface nameOfCustomAnnotation {
   // attributes of annotation come here
}

Example 

In this example, we will create a custom annotation using @Target with the value 'TYPE' which means this annotation is applicable to a class, interface, and enum.

import java.lang.annotation.*;
// declaring the annotations
@Target({ElementType.TYPE})
@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("Annotating an Annotation using @Target");
   }
}

Output

Annotating an Annotation using @Target

Conclusion

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 example programs. We also discovered the use of @Target annotation in annotating another annotation. Note that an annotation just tells us the information about the block of code, it does not affect the working of the whole code.

Updated on: 17-Aug-2023

592 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements