Online Java Compiler By
JavaTpoint.com
import java.lang.*; //import statements? import java.lang.annotation.Annotation; 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 Demo3 { String str(); int val(); } public class ClassgetAnnotationsExample2 { @Demo3(str = "Demo Annotation", val = 100) public static void sample() { ClassgetAnnotationsExample2 obj = new ClassgetAnnotationsExample2(); try { Class c = obj.getClass(); Method m = c.getMethod("sample"); Annotation[] annotation = m.getAnnotations(); for (int i = 0; i < annotation.length; i++) { System.out.println(annotation[i]); } } catch (NoSuchMethodException exc) { exc.printStackTrace(); } } public static void main(String args[]) { sample(); } }
Output