Java SE API
Class.getAnnotation()
Find an annotation or return null if absent. See official API for exceptions and overloads; Java 21-compatible example.
Formal Syntax & Structure
java syntaxjava
<A extends Annotation> A getAnnotation(Class<A> annotationClass)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| annotationClass | Class<A> | Annotation interface |
Return Value
<A extends Annotation> A
Find an annotation or return null if absent.
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 { @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @interface Label { String value(); int version() default 1; } @Label("invoice") static class Invoice {} static class Plain {} public static void main(String[] args) throws Throwable { Label label = Invoice.class.getAnnotation(Label.class); System.out.println(label.value()); System.out.println(label.version()); System.out.println(Plain.class.getAnnotation(Label.class) == null); }}