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