The getAnnotation() method of java.lang.Class class is used to get the annotation of the specified annotation type, if such an annotation is present in this class. The method returns that class in the form of an object.
public T getAnnotation(Class<T> annotationClass)
Parameter: This method accepts a parameter annotationClass which is the type of the annotation to get.
Return Value: This method returns the specified object of the annotation class.
Exception: This method throws:
- NullPointerException: if the given annotation class is null.
Below programs demonstrate the getAnnotation() method.
Class represented by myClass: class Test Annotation of myClass: @Annotation(key=GFG, value=GeeksForGeeks)
Class represented by myClass: class Test Annotation of myClass: null
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.