Java Annotations

Introduction to Annotations

An annotation is a form of tag that represents the metadata. It provides data about a program, which is not a part of the program.

It can be used within classes, methods, packages and variable. Annotations have no direct effect on the function of the code they annotate. It was introduced in Java 1.5.

Uses of Annotations

  • Annotations can be used by the compiler to detect or produce the errors and warning.
  • They can also be used as software tools that can process annotation information to generate code, XML file etc.
  • Some annotations are also available at runtime and are used for different objective like testing, validation, data access etc.

Annotation syntax

An annotation always starts with the symbol @ followed by annotation name.

@Annotation_name

For example: @Override
Where, Override is the name of the annotation.

Built-in Annotation in Java

Annotations used by Java
The predefined annotation defined in java.lang package are given below:

1. @Deprecated
The @Deprecated annotation indicates that the marked elements (class, method or field) are deprecated and should no longer be used. When a class or method is declared with @Deprecated annotation, compiler will give a warning message.

Example : Illustrating the use and effect of @Deprecated annotation

class First
{
       void method1()
       {
             System.out.println("In method1");
       }
      @Deprecated
      void method2()
      {
             System.out.println("In method2");
      }
}
public class AnnotationDemo
{
      public static void main(String args[])
      {
             First obj = new First ();
             obj.method2 ();
      }
}


Output: Compiler generates a warning message.
Note: Without use of @Deprecated annotation, the program will compile and run successfully and gives the output as In method2.

2. @Override
This annotation informs the compiler that the sub class method is overriding the super class method. If a method is declared with @Override annotation and fails to correctly override a method, then the compiler generates a warning.

Example : Program to illustrate the use and effect of @Override annotation

class Vehicle
{
       void running()
       {
             System.out.println("Vehicle is running: ");
       }
}
class Bike extends Vehicle
{
       @Override
       void Running()
       {
             System.out.println("Bike is running.");
       }
}
public class AnnotationDemo
{
       public static void main(String args[])
       {
             Vehicle obj = new Bike();
             obj.running();
       }
}


Output: Compile time error
Note: When we do not use @Override annotation, the program simply compiles and runs successfully.

3. @SuppressWarnings
@SuppressWarnings annotation is used to suppress specific warning generated by the compiler.

Example

@SuppressWarnings("deprecation")
void myMethod()
{
       obj.deprecatedMethod();
}

Creating Custom Annotation

In Java it is possible to create our own annotations. The custom annotations are created by using @interface followed by annotation name.

Example

@interface Annotaion_name
{
      int value1();
      String value2 ();
      String value3 ();
}

Annotations that Apply to Other Annotations

These annotations are also called as meta-annotations because they can apply to other annotations as well. The meta-annotations are defined in java.lang.annotation package

1. @Retention : @Retention annotation is used to specify how the marked annotation is stored.

  • RetentionPolicy.SOURCE
  • It is available only at runtime. It refers to source code and is ignored by the compiler.
  • RetentionPolicy.CLASS
  • It is maintained only by the compiler but ignored by the JVM. It refers to .class file.
  • RetentionPolicy.RUNTIME
  • This annotation is retained by the JVM at runtime.
2. @Documented : @Documented annotation indicate that whenever an element uses this annotation, it must be documented by Javadoc tool.

3. @Target : @Target annotations specify which type of annotation should be used where.  @Target annotation specifies the following possible target value.

  • ElementType.ANNOTATION_TYPE
  • ElementType.CONSTRUCTOR
  • ElementType.FIELD
  • ElementType.LOCAL_VARIABLE
  • ElementType.METHOD
  • ElementType.PACKAGE
  • ElementType.PARAMETER
  • ElementType.TYPE
4. @Inherited : @Inherited annotations specify that the annotation type can be inherited from the super class.

5. @Repeatable : @Repeatable annotation can be applied more than once to the same declarations. It was introduced in Java SE 8.