Title: Annotations in Java | TheCodeForge
Open Graph Title: Annotations in Java | TheCodeForge
Description: Default CLASS retention strips annotations from JVM memory. Run javap -verbose to check RuntimeVisibleAnnotations when reflection fails.
Open Graph Description: Default CLASS retention strips annotations from JVM memory. Run javap -verbose to check RuntimeVisibleAnnotations when reflection fails.
Opengraph URL: https://thecodeforge.io/java/annotations-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/annotations-java/"
},
"headline": "Java @Retention — Why isAnnotationPresent() Returns False",
"description": "Default CLASS retention strips annotations from JVM memory. Run javap -verbose to check RuntimeVisibleAnnotations when reflection fails.",
"keywords": "Java @Retention — Why isAnnotationPresent() Returns False, Java, Intermediate, Advanced Java, Default CLASS retention strips annotations from JVM memory. Run javap -verbose to check RuntimeVisibleAnnotations when reflection fails.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-09-23T00:00:00+00:00",
"url": "https://thecodeforge.io/java/annotations-java/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java @Retention — Why isAnnotationPresent() Returns False", "item": "https://thecodeforge.io/java/annotations-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the difference between @Retention SOURCE, CLASS, and RUNTIME in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "SOURCE annotations are erased by the compiler immediately after it processes them — they never appear in the .class file and are invisible at runtime. Used for compile-time checks like @Override and build-time code generation like Lombok's @Data. CLASS annotations are stored in the .class file under RuntimeInvisibleAnnotations but are stripped when the JVM loads the class into memory — invisible to reflection, usable only by bytecode tools that process .class files directly. CLASS is the default when you omit @Retention, which makes it a common trap. RUNTIME annotations are stored under RuntimeVisibleAnnotations and remain accessible via reflection throughout the program's lifetime. Every annotation you read with isAnnotationPresent() or getAnnotation() must use RUNTIME."
}
},
{
"@type": "Question",
"name": "Can Java annotations have methods?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Annotation elements look like abstract method declarations syntactically, but they define the data attributes the annotation carries rather than executable behavior. Each element can have a default value with the 'default' keyword. The element types are strictly restricted to: primitives (int, long, boolean, etc.), String, Class>, enum values, other annotation types, and one-dimensional arrays of any of the above. You cannot use List, Map, generic types, or arbitrary objects as annotation element types. You also cannot define constructors or method bodies inside an @interface declaration."
}
},
{
"@type": "Question",
"name": "What is the difference between an annotation and an interface in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "An annotation is declared with @interface and implicitly extends java.lang.annotation.Annotation. Unlike a regular interface, annotation types have strict constraints: elements can only be of the restricted types listed above (no generic parameters, no arbitrary objects), elements cannot have implementations or bodies, and annotations cannot extend other annotations or interfaces. The key functional distinction is purpose: interfaces define behavior that classes implement and override; annotations define metadata that tools and frameworks read. A class doesn't implement an annotation — it carries it as a label."
}
},
{
"@type": "Question",
"name": "How do I debug an annotation that seems invisible at runtime?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Three steps, in order. First, run javap -verbose YourClass.class and search for your annotation name in the output. Under RuntimeVisibleAnnotations means RUNTIME retention — reflection should see it. Under RuntimeInvisibleAnnotations means CLASS retention — add @Retention(RetentionPolicy.RUNTIME). Not present at all means SOURCE retention — the compiler erased it. Second, write a unit test that calls isAnnotationPresent(YourAnnotation.class) on a test method or field and asserts true — run this test to get a definitive pass or fail. Third, if the annotation is present but reading wrong values, add debug logging immediately after getAnnotation() that prints every element value — confirm what's actually stored vs what you expect."
}
},
{
"@type": "Question",
"name": "Why does Spring see annotations that vanilla Java reflection misses?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Spring's AnnotationUtils and AnnotatedElementUtils implement transitive annotation lookup that goes well beyond what java.lang.reflect.AnnotatedElement provides. Java's @Inherited only propagates class-to-subclass for class-level annotations. Spring additionally searches interface hierarchies, finds annotations on overriding methods, supports composed annotations (meta-annotations on annotations), and merges annotation attributes across the inheritance chain. This is why @Transactional on an interface method is processed by Spring even though element.getAnnotation() on a concrete class implementing that interface returns null. When you need this behavior outside Spring, you must traverse the type hierarchy manually or use Spring's utilities directly."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large