Annotations

Annotations

Thinking of Annotations in Java, what comes to our mind are different types of annotations and their uses. But what exactly does an annotation do?

Annotations provide additional information to the existing source code without affecting or modifying it. In addition to built-in annotations, we can also create custom annotations based on our needs. We use the mechanism of interface creation to define a custom annotation.

When creating an annotation, we need to specify the target to which it can be applied. We also define the retention policy to determine at what phase (e.g., source code, compile time, runtime) the annotation should be retained.

 

Custom Annotation Example:

 

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

 

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface JavaDeveloper {

     int experience() default 0;

}

 

Let say, we have an Employee class to which this @JavaDeveloper annotaion is applied. Then this add the information that this particulat employee is a Java developer of experience 0. We can also specify the experience inside round brackets which will override the default value 0.

 

Eg:

 

@JavaDeveloper (experience= 5)

public class Employee{

       

}

 

In Java, there are 3 types of retention policies that can be used for annotations.

 

1. RetensionPolicy.SOURCE: These are availble only in the source code and and are discarded by the compiler

2. RetensionPolicy.CLASS: These are availble in the compiled bytecode but are not accessible in the runtime. This is the default Retension Policyy when not explicitly specified.

3. RetensionPolicy.RUNTIME: These are available at the runtime.

 

Below given Targets are some of the examples that we can use in our custom annotation.

 

1. ElementType.TYPE: To be applied on class level

2. ElementType.FIELD: To be applied to instance variables.

3. ElementType.METHOD: To be applied to method level.

4. ElementType.PARAMETER: To be applied to method parameters

5. ElementType.CONSTRUCTOR: To be applied to constructors.

6. ElementType.LOCAL_VARIABLE: To be applied to local variables inside methods.

 

We can use Reflection API to inspect and access the annotation values at runtime given its retention policy is RUNTIME.

public class Main {

public static void main(String[] args) {

Class<Employee> myClass = Employee.class;

    JavaDeveloper annotation = myClass.getAnnotation(JavaDeveloper.class);

     

    if (annotation != null) {

      int value = annotation.experience();

      System.out.println("Experience value: " + value);

    }

}


}

Output:

No alt text provided for this image
Github link:

https://github.com/sreelakshmiks93/telusko_challenge/tree/main/day6/telusko/com 

To view or add a comment, sign in

More articles by Sreelakshmi ks

  • Memoization

    Memoization is a programming technique to optimize processing time over memory space. This technique makes use of a…

Others also viewed

Explore content categories