Annotations in Java (original) (raw)

Annotations in Java are a form of metadata that provide additional information about the program. They do not change the action of a compiled program but can be used by the compiler or runtime for processing.

Key Points

Hierarchy of Annotations in Java

Categories of Annotations

There are broadly 5 categories of annotations as listed:

  1. Marker Annotations
  2. Single value Annotations
  3. Full Annotations
  4. Type Annotations
  5. Repeating Annotations

Let us discuss and we will be appending code wherever required if so.

**1. Marker Annotations

**Example

@TestAnnotation()

**2. Single value Annotations

**Example

@TestAnnotation(“testing”);

**3. Full Annotations

**Example

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

**4. Type Annotations

**Example: Java Program to Demonstrate Type Annotation

Java `

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 = "I am 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;
}

}

`

Output

I am annotated with a type annotation This function's return type is annotated

**5. Repeating Annotations

**Example: Java Program to Demonstrate a Repeatable Annotation

Java `

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(); }

}

`

Output

@MyRepeatedAnnos({@Words(value=1, word="First"), @Words(value=2, word="Second")})

**Standard (Predefined) Annotations

Java provides seven built-in annotations.

From java.lang

**1. @Deprecated

**Example:

Java `

public class DeprecatedTest { @Deprecated public void Display() { System.out.println("Deprecatedtest display()"); }

public static void main(String args[])
{
    DeprecatedTest d1 = new DeprecatedTest();
    d1.Display();
}

}

`

Output

Deprecatedtest display()

**2. @Override

**Example: Java Program to Illustrate Override Annotation

Java `

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()"); } }

`

**3. @SuppressWarnings

**Example: Java Program to illustrate SuppressWarnings Annotation

Java `

class DeprecatedTest { @Deprecated public void Display() { System.out.println("Deprecatedtest display()"); } }

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(); } }

`

Output

Deprecatedtest display()

From java.lang.annotation

1. @Documented

2. @Target

**3. @Inherited

User-defined (Custom) Annotation

User-defined annotations can be used to annotate program elements, i.e. variables, constructors, methods, etc. These annotations can be applied just before the declaration of an element (constructor, method, classes, etc).

**Syntax

[Access Specifier] @interface

{
DataType () [default value];
}

Do keep these certain points as rules for custom annotations before implementing user-defined annotations.

**Example: Java Program to Demonstrate User-defined Annotations

Java `

package source;

import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;

// User-defined annotation @Documented @Retention(RetentionPolicy.RUNTIME) @ interface TestAnnotation { String Developer() default "Rahul"; String Expirydate(); } // will be retained at runtime

// Driver class that uses @TestAnnotation public class Test { @TestAnnotation(Developer="Rahul", Expirydate="01-10-2020") void fun1() { System.out.println("Test method 1"); }

@TestAnnotation(Developer="Anil", Expirydate="01-10-2021")
void fun2()
{
    System.out.println("Test method 2");
}

public static void main(String args[])
{
    System.out.println("Hello");
}

}

`