Defining Annotations: Elements and Defaults
Create custom annotations with @interface, define elements with default values, and apply meta-annotations.
Defining Annotations: Elements and Defaults is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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};
}Applying the Annotation
Apply the annotation to the target. Omit elements that have defaults. When only one element is named value, it can be set without the key.
@Retry(times = 5, delayMs = 500)
public void callExternalService() { ... }
// Single-element shorthand:
@interface Label { String value(); }
@Label("production")
public class App {}Annotation Element Types
Elements can only be: primitives (int, long, etc.), String, Class, an enum, another annotation type, or a single-dimensional array of the above.
@interface Config {
String host();
int port() default 8080;
LogLevel level() default LogLevel.INFO; // enum
Class<?> handler() default DefaultHandler.class;
String[] tags() default {};
}Meta-Annotations: @Retention
@Retention controls how long the annotation is kept: SOURCE (discarded by compiler), CLASS (in .class, not at runtime), RUNTIME (readable via reflection).
@Retention(RetentionPolicy.RUNTIME)
@interface Retry {
int times() default 3;
}Meta-Annotations: @Target
@Target restricts where the annotation can be applied. Multiple targets can be listed.
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface Audited {
String description() default "";
}@Documented and @Inherited
@Documented includes the annotation in Javadoc. @Inherited allows subclasses to inherit the annotation from their superclass (only for class-level annotations).
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Category {
String value();
}@Repeatable Annotations
Mark an annotation @Repeatable to allow it to appear multiple times on the same element. Provide a container annotation that holds an array.
@Repeatable(Tags.class)
@interface Tag { String value(); }
@interface Tags { Tag[] value(); }
@Tag("fast")
@Tag("stable")
public class Service {}Default Values and Omitting Elements
Elements with default values do not need to be specified at the call site. Elements without defaults are mandatory.
@Retry // uses defaults: times=3, delayMs=1000
public void action1() {}
@Retry(times=1) // overrides only times; delayMs stays 1000
public void action2() {}Annotation vs Marker Interface
Marker interfaces (like Serializable) are checked with instanceof and convey type information. Annotations are more flexible — they carry metadata and can have elements.
Common Built-in Annotations
Know the JDK annotations: @Override, @Deprecated, @FunctionalInterface, @SuppressWarnings, and @SafeVarargs.
@Override
public String toString() { return "User{" + name + "}"; }
@Deprecated(since = "2.0", forRemoval = true)
public void oldMethod() {}Quick Check
What is the only retention policy that makes an annotation readable at runtime?
Recap
Declare annotations with @interface. Use @Retention(RUNTIME) for reflective processing, @Target to restrict usage, and default for optional elements. @Repeatable allows stacking.
Frequently asked questions
Is the “Defining Annotations: Elements and Defaults” lesson free?
Yes — the full text of “Defining Annotations: Elements and Defaults” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining Annotations: Elements and Defaults”?
Create custom annotations with @interface, define elements with default values, and apply meta-annotations. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Annotations: Elements and Defaults” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Annotations: Elements and Defaults
- Retention Policies and Target Types
- Runtime Annotation Processing
- Compile-Time Annotation Processors