Java Annotations

In this tutorial, we will realize what annotations are, distinctive Java annotations and how to use them with the help of examples.

Java annotations are metadata (data about data) for our program source code.

They give extra data about the program to the compiler yet are not part of the program itself. These annotations don’t influence the execution of the compiled program.

Annotations start with @. Its syntax is:

@AnnotationName

Let’s take an example of @Override annotation.

The @Override annotation determines that the strategy that has been set apart with this annotation overrides the technique for the superclass with a similar technique name, return type, and parameter list.

It isn’t mandatory to use @Override while overriding a technique. However, if we use it, the compiler gives an error if something isn’t right, (for example, wrong boundary type) while overriding the method.

Example 1: @Override Annotation Example

class Animal {
  public void displayInfo() {
    System.out.println("I am an animal.");
  }
}

class Dog extends Animal {
  @Override
  public void displayInfo() {
    System.out.println("I am a dog.");
  }
}

class Main {
  public static void main(String[] args) {
    Dog d1 = new Dog();
    d1.displayInfo();
  }
}

Output

I am a dog.

In this example, the technique displayInfo() is available in both the superclass Animal and subclass Dog. At the point when this strategy is called, the technique for the subclass is called rather than the strategy in the superclass.


Annotation formats

Annotations may also include elements (members/attributes/parameters).

1. Marker Annotations

Marker annotations do not contain members/elements. It is only used for marking a declaration.

Its syntax is:

@AnnotationName()

Since these annotations do not contain elements, parentheses can be excluded. For example,

@Override

2. Single element Annotations

A single element annotation contains only one element.

Its syntax is:

@AnnotationName(elementName = "elementValue")

If there is only one element, it is a convention to name that element as value.

@AnnotationName(value = "elementValue")

In this case, the element name can be excluded as well. The element name will be value by default.

@AnnotationName("elementValue")

3. Multiple element Annotations

These annotations contain multiple elements separated by commas.

Its syntax is:

@AnnotationName(element1 = "value1", element2 = "value2")

Annotation placement

Any declaration can be set apart with annotation by setting it over that declaration. As of Java 8, annotations can likewise be put before a sort.

1. Above declarations

As mentioned above, Java annotations can be placed above class, method, interface, field, and other program element declarations.

Example 2: @SuppressWarnings Annotation Example

import java.util.*;

class Main {
  @SuppressWarnings("unchecked")
  static void wordsList() {
    ArrayList wordList = new ArrayList<>();

// This causes an unchecked warning
    wordList.add("worldofitech"); 

    System.out.println("Word list => " + wordList);
  }

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

Output

Word list => [worldofitech]

If the above program is compiled without using the @SuppressWarnings(“unchecked”) annotation, the compiler will still compile the program but it will give warnings like:

Main.java uses unchecked or unsafe operations.
Word list => [worldofitech]

We are getting the warning

Main.java uses unchecked or unsafe operations

because of the following statement.

ArrayList wordList = new ArrayList<>();

This is because we haven’t defined the generic type of the array list. We can fix this warning by specifying generics inside angle brackets <>.

ArrayList<String> wordList = new ArrayList<>();

2. Type annotations

Prior to Java 8, annotations could be applied to declarations only. Presently, type annotations can be used also. This implies that we can put annotations in any place we use a sort.

Constructor invocations

new @Readonly ArrayList<>()

Type definitions

@NonNull String str;

This declaration specifies non-null variable str of type String to avoid NullPointerException.

@NonNull List<String> newList;

This declaration specifies a non-null list of type String.

List<@NonNull String> newList;

This declaration specifies a list of non-null values of type String.

Type casts

newStr = (@NonNull String) str;

extends and implements clause

class Warning extends @Localized Message

throws clause

public String readMethod() throws @Localized IOException

Type annotations enable Java code to be analyzed better and provide even stronger type checks.


Types of Annotations

1. Predefined annotations

@Deprecated
@Override
@SuppressWarnings
@SafeVarargs
@FunctionalInterface

2. Meta-annotations

@Retention
@Documented
@Target
@Inherited
@Repeatable

3. Custom annotations

These annotation types are described in detail in the Java Annotation Types tutorial.


Use of Annotations

Compiler instructions – Annotations can be used for giving instructions to the compiler, detect errors, or suppress warnings. The built-in annotations @Deprecated, @Override,@SuppressWarnings are used for these purposes.

Compile-time instructions – Compile-time instructions provided by these annotations help the software build tools to generate code, XML files, and many more.

Runtime instructions – Some annotations can be defined to give instructions to the program at runtime. These annotations are accessed using Java Reflection.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.