Programs

Annotations in Java: Types, Uses and Examples

An annotation in a programming language such as Java refers to the syntactic metadata that can be integrated with Java’s source code. Various factors such as Java packages, variables, classes, parameters and methods can be annotated. Developers may easily read the Java annotations directly from the source files, just like Javadoc tags.

Java annotations can also be included in and read from the Java compiler’s output Java class files. This enables the Java virtual machine to maintain annotations and read them through a reflection at run-time. In Java, it is possible to build meta-annotations from existing ones.

These are some basics of annotations in Java:

  1. Annotations always start with a ‘@’. For example: ‘@override’, ‘@target’, ‘@SuppressWarnings’, etc.
  2. Annotations do not affect the behaviour of a compiled program.
  3. Annotations aid in associating metadata to program components like methods, classes, variables, etc.
  4. Annotations are not just comments since they can affect how a program is processed by the compiler. They offer additional compiler information about the program but are not part of the program itself. Therefore, these annotations do not affect the compiled program’s execution.

Types of Annotations in Java

1. Single Value Annotations

Single value annotations allow only one shorthand form since they contain only one member. The member must be given a value post-application of the annotation. However, the name of the annotation does not need to be specified. The member must have a value if the shorthand is to be used. For example: 

@TestAnnotation(“testing”);

2. Full Annotations

Full annotations consist of various data such as data members, values, pairs and names. 

For example: 

@TestAnnotation(owner=”Rahul”, value=”Class Geeks”)

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

3. Repeating Annotations

When an annotation can be applied to singular objects more than once, it is labelled as a repeating annotation. Repeating annotations are specified with the @Repeatable tag as defined in the java.lang.annotation package. Its value field indicates the repeated annotation’s container type.

The container is defined as an annotation with a value field that contains an array of repeating annotations. To construct a repeated annotation, first create the container annotation, and then specify the annotation type as an input to the @Repeatable annotation.

For example: 

// Java Program to Demonstrate a Repeatable Annotation

 

// Importing required classes

import java.lang.annotation.Annotation;

import java.lang.annotation.Repeatable;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.reflect.Method;

 

// Make Words annotation repeatable

@Retention(RetentionPolicy.RUNTIME)

@Repeatable(MyRepeatedAnnos.class)

@interface Words

{

    String word() default “Hello”;

    int value() default 0;

}

 

// Create container annotation

@Retention(RetentionPolicy.RUNTIME)

@interface MyRepeatedAnnos

{

    Words[] value();

}

public class Main {

 

    // Repeat Words on newMethod

    @Words(word = “First”, value = 1)

    @Words(word = “Second”, value = 2)

    public static void newMethod()

    {

        Main obj = new Main();

 

        try {

            Class<?> c = obj.getClass();

 

            // Obtain the annotation for newMethod

            Method m = c.getMethod(“newMethod”);

 

            // Display the repeated annotation

            Annotation anno

                = m.getAnnotation(MyRepeatedAnnos.class);

            System.out.println(anno);

        }

        catch (NoSuchMethodException e) {

            System.out.println(e);

        }

    }

    public static void main(String[] args) { newMethod(); }

}

 

The final output would be: @MyRepeatedAnnos(value={@Words(value=1, word=”First”), @Words(value=2, word=”Second”)})

Explore our Popular Software Engineering Courses

3. Marker Annotations

Marker annotations are only meant for declaration purposes. Neither does this annotation contain any members nor does any data exist within it. Its only purpose is to maintain a presence as an annotation. For example: @Override.

4. Type annotations

These annotations can be used everywhere that a type is used. For example, we can annotate a method’s return type. Type annotations are tagged with the @Target annotation. 

Code which can illustrate type annotations are:

// Importing required classes

import java.lang.annotation.ElementType;

import java.lang.annotation.Target;

 

// Using target annotation to annotate a type

@Target(ElementType.TYPE_USE)

 

// Declaring a simple type annotation

@interface TypeAnnoDemo{}

 

// Main class

public class GFG {

   

    // Main driver method

    public static void main(String[] args) {

 

        // Annotating the type of a string

        @TypeAnnoDemo String string = “This code is annotated with a type annotation”;

        System.out.println(string);

        abc();

    }

 

    // Annotating return type of a function

    static @TypeAnnoDemo int abc() {

       

        System.out.println(“This function’s  return type is annotated”);

       

        return 0;

    }

Examples of Predefined Annotations

There are many varieties of predefined annotations which we have mentioned earlier. Let us take a look:

1. @Override

This marker annotation can only be used on methods. Any method with the @override annotation must supersede another method from a superclass. Failing this, a compile-time error gets triggered. This happens since it must ensure that the superclass is actually overridden and not just overloaded.

Code which can illustrate the Override Annotation is: 

// Class 1

class Base

{

     public void Display()

     {

         System.out.println(“Base display()”);

     }

      

     public static void main(String args[])

     {

         Base t1 = new Derived();

         t1.Display();

     }    

}

 

// Class 2

// Extending above class

class Derived extends Base

{

     @Override

     public void Display()

     {

         System.out.println(“Derived display()”);

     }

}

2. @Target

It is only meant to be used as an annotation to another annotation. @Target accepts one parameter, which must be an ElementType constant. The parameter decides which declarations it can be applied to. The @Target annotation can have multiple values instead of just a singular value. However, if multiple values must be declared, then it must be in the form of braces such as: @Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE}).

The @Retention annotation can be used here to determine the retention status of the annotation. Three factors affect a @Retention annotation:

  • Source: The compiler disregards the annotations as they are usually kept at the source level.
  • Runtime: It is retained at runtime.
  • Class: The annotations remain compile-time and are disregarded by the Java program.

3. @Inherited

This annotation can only be used for declaration purposes, and hence, only affects certain class declarations. One annotation can supersede the other and inherit its properties. Thus,  the superclass can be checked if specific annotations are absent from the subclass. However, if the annotation is present, it gets the @Inherited annotation.

For example:

// Class 1

class DeprecatedTest

{

    @Deprecated

    public void Display()

    {

        System.out.println(“Deprecatedtest display()”);

    }

}

// Class 2

public class SuppressWarningTest

{

    // If we comment below annotation, program generates

    // warning

    @SuppressWarnings({“checked”, “deprecation”})

    public static void main(String args[])

    {

        DeprecatedTest d1 = new DeprecatedTest();

        d1.Display();

    }

}

Conclusion

Here, we have learned about annotations in Java. We have also learnt about the types of annotations in Java and their uses which can help Java developers create robust and scalable programs with ease. 

If you’d like to learn more about Java concepts, upGrad’s Job-linked PG Certification in Software Engineering is your best bet to gain a comprehensive understanding of programming languages like Java, HTML, JS, as well as other software development tools and libraries. 

The program is designed for final year students or unemployed graduates looking to secure entry-level positions in the field. The 5-months course covers specialisation in MERN/Cloud-Native and helps students build a top-class portfolio by exposing them to 5 hands-on projects.

So, don’t wait. Head over to upGrad and book your seat today!

What are @jsonproperty annotations?

Jsonproperty annotations allow developers to map JSON keys with property names during serialisation and deserialisation. Based on Java's default settings, keys are mapped to POJO fields during serialisation. Json annotations help override this default functionality by using string attributes that indicate the mapped name.

What are hibernate annotations?

Hibernate Annotations are another technique to specify metadata when mapping the Object and Relational Table. They are a powerful and relatively new mapping method that can be used in place of XML mapping metadata. Using hibernate annotations, users can store metadata in a single POJO java file that also carries the code, thereby helping to better understand a table structure.

What are @SuppressWarnings?

The @SuppressWarnings annotation notifies the compiler of warnings that are suppressed. The notifications are present in string form by name. It can be given to any kind of declaration. Java classifies warnings into two classes, namely unchecked and deprecated. When older code interacts with generics-based code, an unchecked warning is produced.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

×
Let’s do it!
No, thanks.