Annotations in Java

Introduction

Annotations in Java are pieces of code that have more functionality than simple comments but have lesser functionality as compared to actual lines of source code. In simpler words, annotations are used to provide any kind of additional information about the program.

Features of Annotations in Java

  • All annotations must start with ‘@’.
  • Annotations do not affect the action of a compiled code. These lines of code act closer towards comments rather than source code.
  • These annotations are an avenue to associate metadata to the program elements. Metadata normally comprise instance variables, constructors, methods, classes, etc.
  • Annotations should not be confused with comments. While comments are non-executable statements of the compiler, annotations can change the way a program is executed by a compiler.

Sample Code

//The program below shows the functionality of annotations in Java and how they are more than just comments.

class Base
{
public void display ()
{
System.out.println (“ Base Display ”) ;
}
}

class Derived extends Base
{
@Override
public void display (int x)
{
System.out.println (“ Derived Display (int) ”) ;
}

public static void main (String args [])
{
Derived obj = new Derived ();
obj.display ();
}
}

OUTPUT

10: error: method does not override or implement a method from a supertype.

//In such cases, if the parameter (int x) or @override is removed, the program will function just fine.

Types of Annotations

There are three main types of annotations:

  1. Marker Annotations: The marker annotations, as the name suggests, is only used to mark a declaration. Annotations of this form are void of members or data. Therefore, just the annotation in the program is enough for its effect to resonate. As there are no members, just making sure if the annotation is present or absent is enough.
    Example: @TestAnnotation()
    @Override.
  2. Single Value Annotations: Single Value Annotations contain only one member and allow for a shorthand form for stating the value of the member. Whenever the annotation is put into place, only then do we need to explicitly state the name of the member. If you are using the shorthand, it is compulsory that the name of the member has to be ‘value’.
    Example: @TestAnnotation(“testing”);
  3. Full Annotations: Full Annotations are annotations that contain multiple data members/ name, value, and pairs.
    Example; @TestAnnotation(owner=”Rahul”, value = “Class Demo”)
  4. Type Annotations: Whenever a type is associated with some annotation, the type annotations can be used. As a simple example, the return type of a method can be annotated. The @Target command needs to be used in such cases. Example:
    import java.lang.annotation.ElementType;
    import java.lang,annotation.Target;
    
    @Target(ElementType.TYPE_USE)
    @interface TypeAnnoDemo{}
    
    public class Main {
    public static void main (String args [])
    {
    @TypeAnnoDemo String string = “Annotated with Type annotation”;
    out.println(string);
    abc();
    }
    
    static @TypeAnnoDemo int abc()
    {
    System.out.println (“This function’s return type has been annotated”);
    return 0;
    }
    }

    OUTPUT
    Annotated with a type annotation
    This function’s return type has been annotated

  5. Repeating Annotation: As the name suggests, a single item can be annotated multiple times using the Repeating Annotation. This is defined in the java.lang.annotation package, and the value field of this type of annotation states the container type for the repeatable annotation.

In-Built Annotations

Java defined seven in-built annotations. They are listed as follows:

Four annotations imported from java.lang.annotation are:

  • @Retention
  • @Documented
  • @Target
  • @Inherited

Three annotations imported from java.lang are:

  • @Deprecated
  • @Override
  • @SuppressWarnings

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.