René's URL Explorer Experiment


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/

direct link

Domain: thecodeforge.io


Hey, it has json ld scripts:
{
  "@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:typewebsite
og:imagehttps://thecodeforge.io/assets/og-image.png
og:image:width1200
og:image:height630

Links:

TheCodeForge https://thecodeforge.io/
Javahttps://thecodeforge.io/java/
Buy me a coffee https://ko-fi.com/thecodeforge
1Generics in Javahttps://thecodeforge.io/java/generics-java/
2Reflection API in Javahttps://thecodeforge.io/java/reflection-api-java/
3Annotations in Javahttps://thecodeforge.io/java/annotations-java/
4Enums in Javahttps://thecodeforge.io/java/enums-java/
5Inner Classes in Javahttps://thecodeforge.io/java/inner-classes-java/
6Anonymous Classes in Javahttps://thecodeforge.io/java/anonymous-classes-java/
7Design Patterns in Javahttps://thecodeforge.io/java/design-patterns-java/
8Singleton Pattern in Javahttps://thecodeforge.io/java/singleton-pattern-java/
9Factory Pattern in Javahttps://thecodeforge.io/java/factory-pattern-java/
10Builder Pattern in Javahttps://thecodeforge.io/java/builder-pattern-java/
11Garbage Collection in Javahttps://thecodeforge.io/java/garbage-collection-java/
12JVM Memory Modelhttps://thecodeforge.io/java/jvm-memory-model/
13JVM Memory Issues in Production: Debugging Guide (OOM, GC, Leaks)https://thecodeforge.io/java/jvm-memory-debugging/
14JVM GC Tuning Guide: G1, ZGC, Shenandoah Explained with Real Trade-offshttps://thecodeforge.io/java/jvm-gc-tuning/
15Observer Pattern in Javahttps://thecodeforge.io/java/observer-pattern-java/
16Strategy Pattern in Javahttps://thecodeforge.io/java/strategy-pattern-java/
17Decorator Pattern in Javahttps://thecodeforge.io/java/decorator-pattern-java/
18Dependency Injection in Javahttps://thecodeforge.io/java/dependency-injection-java/
19Java Modules — JPMShttps://thecodeforge.io/java/java-modules-jpms/
20Java Memory Leaks and Preventionhttps://thecodeforge.io/java/java-memory-leaks-prevention/
21Spring Boot Introductionhttps://thecodeforge.io/java/spring-boot-introduction/
22Maven vs Gradle in Javahttps://thecodeforge.io/java/maven-vs-gradle-java/
23Java Profiling and Performancehttps://thecodeforge.io/java/java-profiling-performance/
24Java Logging with SLF4J and Logbackhttps://thecodeforge.io/java/java-logging-slf4j-logback/
25Java Agent and Instrumentationhttps://thecodeforge.io/java/java-agent-instrumentation/
26JUnit 5 Annotations: @Test, @BeforeEach, @AfterEach and Morehttps://thecodeforge.io/java/junit5-annotations/
27Mockito verify(): How to Assert Method Calls in Unit Testshttps://thecodeforge.io/java/mockito-verify/
28Unit Testing vs Integration Testing: Key Differenceshttps://thecodeforge.io/java/unit-test-vs-integration-test/
29SLF4J StaticLoggerBinder Warning Fixhttps://thecodeforge.io/java/slf4j-staticloggerbinder/
← Back to Java track https://thecodeforge.io/java/
Homehttps://thecodeforge.io/
Javahttps://thecodeforge.io/java/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
 ● Production Incidenthttps://thecodeforge.io/java/annotations-java/#v2-production-incident
 🔎 Debug Guidehttps://thecodeforge.io/java/annotations-java/#v2-debug-guide
Spring Boothttps://thecodeforge.io/java/spring-boot-docker/
Stackhttps://thecodeforge.io/dsa/stack-data-structure/
dependency injectionhttps://thecodeforge.io/java/dependency-injection-java/
enumshttps://thecodeforge.io/java/enums-java/
inheritancehttps://thecodeforge.io/java/inheritance-java/
containerhttps://thecodeforge.io/dsa/container-with-most-water/
→Java Reflection — setAccessible Fails on JDK 9+setAccessible fails on JDK 9+ internals with InaccessibleObjectException. Our production debug guide shows how to fix mohttps://thecodeforge.io/java/reflection-api-java/
→Method Overriding in Java: Missing @Override Cost PaymentPayments silently processed via generic processor due to missing @Override – finance flagged thousands. Catch at compilehttps://thecodeforge.io/java/method-overriding-java/
→Java Classes & Objects — Heap Leaks from Static ReferencesStatic HashMaps caused OOM after 72 hours by pinning millions of objects. Learn the lifecycle patterns that prevent memohttps://thecodeforge.io/java/classes-objects-java/
→Java Anonymous Classes — this$0 Synthetic Field TrapsNon-static anonymous classes inject a hidden this$0 outer reference. One team leaked 80MB daily via static executor holdhttps://thecodeforge.io/java/anonymous-classes-java/
→Java ExceptionInInitializerError — Fix Static CauseExceptionInInitializerError wraps a static-init failure. Read the Caused by line, fix the static block, and move I/O tohttps://thecodeforge.io/java/java-exceptionininitializererror/
One-Time Pad — Perfect SecrecyIntermediatehttps://thecodeforge.io/dsa/one-time-pad/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←PreviousReflection API in Javahttps://thecodeforge.io/java/reflection-api-java/
NextEnums in Java→https://thecodeforge.io/java/enums-java/
LinkedIn https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
Facebook https://www.facebook.com/profile.php?id=61583020033815
YouTube https://www.youtube.com/channel/UCMar7CF2EmCF1I2tw1tNVuQ
Javahttps://thecodeforge.io/java/
Pythonhttps://thecodeforge.io/python/
C / C++https://thecodeforge.io/c-cpp/
JavaScripthttps://thecodeforge.io/javascript/
DSAhttps://thecodeforge.io/dsa/
Databasehttps://thecodeforge.io/database/
System Designhttps://thecodeforge.io/system-design/
DevOpshttps://thecodeforge.io/devops/
C# / .NEThttps://thecodeforge.io/csharp/
PHPhttps://thecodeforge.io/php/
ML / AIhttps://thecodeforge.io/ml-ai/
Interviewhttps://thecodeforge.io/interview/
CS Fundamentalshttps://thecodeforge.io/cs-fundamentals/
Rusthttps://thecodeforge.io/rust/
Toolshttps://thecodeforge.io/tools/
Cheatsheetshttps://thecodeforge.io/cheatsheets/
Mobilehttps://thecodeforge.io/mobile/
Data Analyticshttps://thecodeforge.io/data-analytics/
Securityhttps://thecodeforge.io/security/
Frontendhttps://thecodeforge.io/frontend/
Data Engineeringhttps://thecodeforge.io/data-engineering/
Testinghttps://thecodeforge.io/testing/
Cloudhttps://thecodeforge.io/cloud/
Observabilityhttps://thecodeforge.io/observability/
Web Platformhttps://thecodeforge.io/web-platform/
🧯 Errors Explainedhttps://thecodeforge.io/errors/
⚒ Blueprintshttps://thecodeforge.io/blueprints/
⌨ Practicehttps://thecodeforge.io/practice/
Interview Prephttps://thecodeforge.io/interview/
DSA Problemshttps://thecodeforge.io/dsa/
Developer Toolshttps://thecodeforge.io/tools/
Cheat Sheetshttps://thecodeforge.io/cheatsheets/
Abouthttps://thecodeforge.io/about/
Sitemaphttps://thecodeforge.io/sitemap.xml
Privacyhttps://thecodeforge.io/privacy.html

Viewport: width=device-width, initial-scale=1.0

Robots: max-image-preview:large


URLs of crawlers that visited me.