@Target specifies where an annotation can be applied. @Retention determines how long the annotation is retained.
The @Target annotation defines the contexts (e.g., method, field, class) where your custom annotation can be used. This prevents misuse by restricting its application scope. The @Retention annotation, on the other hand, specifies whether the annotation is available at runtime or only during source code compilation. For instance, @Retention(RetentionPolicy.RUNTIME) allows annotations to be accessible via reflection during runtime, enabling dynamic behaviors in frameworks. Consider a custom annotation @Loggable to log method execution. Using @Target(ElementType.METHOD) limits its application to methods, while @Retention(RetentionPolicy.RUNTIME) ensures it's available for runtime processing.
Additional Notes
How do @Target and @Retention annotations influence custom annotations?