Skip to content
Related Articles

Related Articles

Field getAnnotation() method in Java With Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Sep, 2019

The getAnnotation() method of java.lang.reflect.Field is used to return returns Field objects’s for the specified type if such an annotation is present, else null.This is important method to get annotation for Field object.

Syntax:

public <T extends Annotation> T
  getAnnotation(Class<T> annotationClass)

Parameters: This method annotationClass which is the Class object corresponding to the annotation type.

Return: This method returns this element’s annotation for the specified annotation type if present on this element, else null.

Exception: This method throws NullPointerException if the given annotation class is null.

Below programs illustrate getAnnotation() method:
Program 1:




// Java program to illustrate
// getAnnotation() method
  
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.util.Arrays;
  
public class GFG {
  
    // initialize field with
    // default value in annotation
    @annotations(3125462345.32155365326)
  
    private double realNumbers;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
  
        // create Field object
        Field field
            = GFG.class
                  .getDeclaredField("realNumbers");
  
        // apply getAnnotation()
        annotations annotations
            = field.getAnnotation(
                annotations.class);
  
        // print results
        System.out.println(annotations);
    }
  
    @Target({ ElementType.FIELD })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface annotations {
        double value() default 99.9;
    }
}

Output:

@GFG$annotations(value=3.1254623453215537E9)

Program 2:




// Java program to illustrate
// getAnnotation() method
  
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.util.Arrays;
  
public class GFG {
    private int @SpecialNumber[] number;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("number");
  
        // apply getAnnotation() method
        AnnotatedType annotatedType
            = field.getAnnotation();
  
        // print the results
        System.out.println(
            "Type: "
            + annotatedType.getType()
                  .getTypeName());
        System.out.println(
            "Annotations: "
            + Arrays.toString(
                  annotatedType
                      .getAnnotations()));
        System.out.println(
            "Declared Annotations: "
            + Arrays.toString(
                  annotatedType
                      .getDeclaredAnnotations()));
    }
  
    @Target({ ElementType.TYPE_USE })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface SpecialNumber {
    }
}

Output:

@GFG$annotations(value=WelcomeTOGFG)

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getAnnotation–


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!