Java Reflection - Java Class.getAnnotation(Class <A> annotationClass)








Syntax

Class.getAnnotation(Class <A> annotationClass) has the following syntax.

public <A extends Annotation> A getAnnotation(Class <A> annotationClass)

Example

In the following code shows how to use Class.getAnnotation(Class <A> annotationClass) method.

public class Main{
  public static void main(String[] unused) {
    try {/*from  w w w . j a  va  2s. com*/
      String n = "java.lang.Deprecated";
      Class c = Class.forName(n);
      Class d = Class.forName("java.util.Date");
      System.out.println(d.getAnnotation(c));

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

}

The code above generates the following result.