0Pricing
Java Academy · Lesson

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

  1. Defining Annotations: Elements and Defaults
  2. Retention Policies and Target Types
  3. Runtime Annotation Processing
  4. Compile-Time Annotation Processors
← Back to Java Academy