Annotations and Metadata

Annotations attach metadata to declarations. Frameworks can define custom annotations and inspect them at compile time or runtime.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Audited {
    String action();
}

Retention controls whether metadata is available in source, class files, or runtime reflection. Annotation processing can generate code during compilation and avoid some runtime reflection.

Key Takeaways
  • Annotations are metadata, not executable behavior by themselves.
  • Retention determines how long annotation information is available.
  • Compile-time processing can move work out of runtime.