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:
Output:
[@GFG$annotations(value=32512.21)]
Program 2: