Field getAnnotation() method in Java With Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
    }
}

chevron_right


Output:

@GFG$annotations(value=3.1254623453215537E9)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 {
    }
}

chevron_right


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

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.