Java SE API

Class.getDeclaredAnnotation()

Return directly present annotation or null, ignoring inheritance. See official API for exceptions and overloads; Java 21-compatible example.

Formal Syntax & Structure

java syntaxjava
<A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass)

Parameters & Attributes

NameTypeDescription
annotationClassClass<A>Annotation interface

Return Value

<A extends Annotation> A

Return directly present annotation or null, ignoring inheritance.

Working Example

Example Snippetjava
import java.lang.annotation.*;import java.lang.reflect.*;import java.lang.invoke.*;import java.util.*;import java.nio.file.*;import java.nio.charset.StandardCharsets;import java.net.URLClassLoader;import java.net.URL;public class Main {    @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE)    @interface Reviewed {}    @Reviewed static class Parent {}    static class Child extends Parent {}    public static void main(String[] args) throws Throwable {        System.out.println(Child.class.isAnnotationPresent(Reviewed.class));        System.out.println(Child.class.getDeclaredAnnotation(Reviewed.class) == null);    }}
Java Reference