Online Java Compiler By
JavaTpoint.com
//import statement import java.lang.*; //import lang package import java.lang.annotation.Retention; //import Retention import java.lang.annotation.RetentionPolicy; //import Retention Policy import java.lang.reflect.Method; //import Method class @Retention(RetentionPolicy.RUNTIME) @interface DemoClassX1 { String str(); int val(); } public class ReflectMethodgetAnnotationExample1 { @DemoClassX1(str = "Demo_Annotation", val = 100) public static void example() { ReflectMethodgetAnnotationExample1 obj = new ReflectMethodgetAnnotationExample1(); try { Class c = obj.getClass(); //get the class of object Method m = c.getMethod("example"); DemoClassX1 annotation = m.getAnnotation(DemoClassX1.class); System.out.println(annotation.str() + " " + annotation.val()); //print statemet } catch (NoSuchMethodException exc) { exc.printStackTrace(); } } public static void main(String args[]) { example(); } }
Output