Class getDeclaredAnnotation() method in Java with Examples
The getDeclaredAnnotation() method of java.lang.Class class is used to get the declared annotation of the specified declared annotation type, if such an declared annotation is present in this class. The method returns that class in the form of an object.
public T getDeclaredAnnotation(Class<T> declared annotationClass)
Parameter: This method accepts a parameter declared annotationClass which is the type of the declared annotation to get.
Return Value: This method returns the specified object of the declared annotation class.
Exception: This method throws:
- NullPointerException: if the given declared annotation class is null.
Below programs demonstrate the getDeclaredAnnotation() method.
Class represented by myClass: class Test
DeclaredAnnotation of myClass: @DeclaredAnnotation(key=GFG, value=GeeksForGeeks)
Class represented by myClass: class Test DeclaredAnnotation of myClass: @java.lang.Deprecated()
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.