Java - Annotations & Reflection

Annotations & Reflection

Defining and Using Annotations

import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Info { String value(); }

@Info("demo")
class Demo {}

Reading Annotations via Reflection

Info info = Demo.class.getAnnotation(Info.class);
System.out.println(info.value()); // demo
Architect note: Prefer annotations for declarative metadata; keep reflection centralized and tested.