// Annotation class import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.FIELD}) public @interface MyCustomAnnotation { String name(); int age(); } // Annotated class @MyCustomAnnotation(name = "ABC", age = 10) public class AnnotatedClass { // Class implementation } // Driver Class/ Main class import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class MainApp { public static void main(String[] args) { // Get the class Class<AnnotatedClass> annotatedClass = AnnotatedClass.class; // Get all annotations on the class Annotation[] annotations = annotatedClass.getAnnotations(); // Iterate over the annotations for (Annotation annotation : annotations) { if (annotation instanceof MyCustomAnnotation) { // Access annotation elements MyCustomAnnotation customAnnotation = (MyCustomAnnotation) annotation; String name = customAnnotation.name(); int age = customAnnotation.age(); // Display annotation values System.out.println("Name: " + name); System.out.println("Age: " + age); } } } } [Access Specifier] @interface<AnnotationName> { DataType <Method Name>() [default value]; }