Defining Annotations: Elements and Defaults
Create custom annotations with @interface, define elements with default values, and apply meta-annotations.
What Are Annotations?
Annotations are metadata applied to classes, methods, fields, or parameters. They do not change behavior directly — they provide information to compilers, frameworks, or runtime processors.
Declaring an Annotation with @interface
Use @interface to declare a custom annotation. Elements look like methods. The default value is specified with default.
@interface Retry {
int times() default 3;
long delayMs() default 1000L;
Class<? extends Throwable>[] on() default {Exception.class};
}All lessons in this course
- Defining Annotations: Elements and Defaults
- Retention Policies and Target Types
- Runtime Annotation Processing
- Compile-Time Annotation Processors