Module 14: Enums and Annotations
Creating Simple Custom Annotations
Create simple custom annotations so metadata becomes part of your design.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-17
Java version
Java 25 LTS
Learning goals
- Declare a custom annotation with `@interface`
- Choose retention and target rules intentionally
- Understand where custom annotations are actually useful
Custom annotations let you describe code with your own metadata: That metadata can later be read by tools, reflection, or internal frameworks.
Retention matters: If the annotation must exist at runtime, choose RetentionPolicy.RUNTIME. If it is only for source tooling, source retention may be enough.
Target matters too: Limit the annotation to classes, methods, fields, or parameters depending on the purpose.
Keep the use case concrete: Validation markers, route metadata, and lightweight documentation are more useful than decorative annotations with no behavior behind them.
Runnable examples
A simple runtime annotation
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface RequiresReview {
String value();
}Expected output
The annotation stays available at runtime for reflection-based checks.
Mini exercise
Create a `@Todo` or `@RequiresReview` annotation with one string value.
Summary
- Custom annotations add metadata you control.
- Retention and target are core design choices.
- Annotations are useful when some tool or code will actually consume them.
Next step
Finish the intermediate track with a state-machine project that combines enums and annotations.
Sources used