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:
// 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; } } |
@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 { } } |
@GFG$annotations(value=WelcomeTOGFG)
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getAnnotation–
Recommended Posts:
- Field get() method in Java with Examples
- Field set() method in Java with Examples
- Field setShort() method in Java with Examples
- Field toGenericString() method in Java with Examples
- Field setDouble() method in Java with Examples
- Field isSynthetic() method in Java with Examples
- Field getByte() method in Java with Examples
- Field getAnnotationsByType() method in Java With Examples
- Field getDeclaredAnnotations() method in Java With Examples
- Field setFloat() method in Java with Examples
- Field getLong() method in Java with Examples
- Field hashCode() method in Java with Examples
- Field getType() method in Java with Examples
- Field toString() method in Java with Examples
- Field getName() method in Java with Examples
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.



