Field getAnnotationsByType() method in Java With Examples

The getAnnotationsByType() method of java.lang.reflect.Field is used to return annotations that are associated with this field element. This is an important method to get annotation for Field object. If there are no annotations associated with this Field element, the return empty array. The caller can modify the returned array as method sent a copy of the actual object; it will have no effect on the arrays returned to other callers.

Syntax:

public <T extends Annotation> T[] 
  getAnnotationsByType(Class<T> annotationClass)

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



Return: This method returns all this element’s annotations for the specified annotation type if associated with this element, else an array of length zero.

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

Below programs illustrate getAnnotationsByType() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[@GFG$annotations(value=32512.21)]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[@GFG$annotations(value=ChipherCodes@#!)]

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getAnnotationsByType-java.lang.Class-



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.