To create a custom annotation in Java, declare it with the @interface keyword, add the meta-annotations @Retention and @Target, and define its elements as methods. To make the annotation do something, read it at runtime with reflection or process it at compile time with an annotation processor.
An annotation by itself changes nothing. It is metadata, and a framework or your own code has to look for it. That is how Spring, JUnit and Jakarta EE turn a few annotations into behaviour.
An annotation type looks like an interface, but it is declared with @interface. Each method is an element of the annotation, and it can have a default value.
Element types are limited to primitives, String, Class, enums, other annotations, and one-dimensional arrays of those. An element named value is special: when it is the only element you set, you can write @Audited("x") without the name.
Two meta-annotations control how long the annotation lives and where it can be placed.
RetentionPolicy | Kept in | Typical use |
|---|---|---|
SOURCE | Source code only | Compiler hints, Lombok-style processors |
CLASS | Class file, not at runtime (the default) | Bytecode tools |
RUNTIME | Class file and visible to reflection | Frameworks, validation, custom logic |
@Target takes one or more ElementType values such as TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR, RECORD_COMPONENT and TYPE_USE. If you omit @Target, the annotation can be applied to most declarations. Other useful meta-annotations are @Inherited (subclasses inherit a class-level annotation), @Repeatable (allow the same annotation more than once) and @Documented (include it in Javadoc).
Only createOrder carries the metadata. Annotations on a method are never inherited by overriding methods, so check the exact method you care about.
At runtime, Method.getAnnotation returns the annotation instance, or null when it is absent. This works only for RUNTIME retention.
Real code usually combines this with a dynamic proxy or an aspect, so the annotated method is intercepted automatically rather than scanned by hand.
Annotations on fields are the basis of many small validation or mapping tools. This example checks that fields marked @NotBlankField are not empty:
Records work with annotations, but the target of a record component annotation is propagated to every element it applies to (field, accessor, constructor parameter), so choose @Target values deliberately. Calling setAccessible(true) on classes in other modules can fail under the Java module system, which is another reason to prefer a framework for real validation.
When you want errors at build time rather than at runtime, write an annotation processor that extends javax.annotation.processing.AbstractProcessor and register it with META-INF/services/javax.annotation.processing.Processor. The compiler calls it with the annotated elements, and it can generate code or report errors through Messager. Processors power Dagger, MapStruct and many others. Keep them out of your application jar by declaring them as annotationProcessor (Gradle) or in the compiler plugin's annotationProcessorPaths (Maven).
The most common reason is that the retention is not RUNTIME. The default is CLASS, so the annotation is not visible to reflection unless you set it explicitly.
No. An annotation type cannot extend another class or annotation, and it cannot declare generics. Use composition by annotating your annotation with another one (a meta-annotation), which is how Spring builds composed annotations.
Yes, if you mark it @Repeatable and provide a container annotation. Read the results with getAnnotationsByType.
Reading annotations has a small cost, but frameworks cache the result. Scan once at startup and keep the metadata instead of reflecting on every call.
Declare an annotation with @interface, give it RUNTIME retention when you need to read it with reflection, and restrict its placement with @Target. The annotation only carries metadata, so pair it with reflection, a proxy, an aspect or a compile-time processor to give it behaviour.