import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String value(); } @MyAnnotation("ExampleAnnotation") public class MyClass { public static void main(String[] args) { boolean annotated = MyClass.class.isAnnotationPresent(MyAnnotation.class); System.out.println("MyClass is annotated with MyAnnotation? " + annotated); } }
import java.lang.annotation.*; public class MyClass { @Deprecated public void doSomething() { //... } public static void main(String[] args) { boolean annotated = MyClass.class.getDeclaredMethod("doSomething").isAnnotationPresent(Deprecated.class); System.out.println("doSomething is deprecated? " + annotated); } }This code defines a deprecated method, doSomething(), and uses isAnnotationPresent() to determine if it is annotated with the @Deprecated annotation. Package Library: The isAnnotationPresent() method is part of the Java Reflection API, which is included in the java.lang package.