Last Updated: June 6, 2026
Every annotation you write has a lifespan. Some annotations disappear the moment the compiler has finished checking them. Others live in the .class file but never reach the running JVM. A third group sticks around all the way through and can be read at runtime through reflection. That lifespan is controlled by a single meta-annotation, @Retention, and the three values of java.lang.annotation.RetentionPolicy: SOURCE, CLASS, and RUNTIME. Picking the wrong one wastes space at best and breaks the framework at worst.
This lesson walks through where each policy drops off the lifecycle, what javap reveals in each case, why CLASS is the default, and how to choose between the three when you design your own annotations. Three small E-Commerce annotations carry the examples: @PriceFormat for a lint check, @StorageKey for a bytecode rewriter, and @ValidateOrder for a reflection-based validator.