Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you create a custom annotation in Java?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedOct 19, 2024
UpdatedNov 16, 2026
5 min read

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.

Table of Contents

  • Declaring an annotation with @interface
  • Choosing the retention policy and target
  • Applying the annotation
  • Reading the annotation with reflection
  • Applying a custom annotation to fields
  • Compile-time processing
  • Frequently Asked Questions
    • Why does getAnnotation return null?
    • Can an annotation have inheritance or implement interfaces?
    • Can I use the same annotation twice on one element?
    • Is reflection slow for this?
  • Summary

Declaring an annotation with @interface

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.

java

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.

Choosing the retention policy and target

Two meta-annotations control how long the annotation lives and where it can be placed.

RetentionPolicyKept inTypical use
SOURCESource code onlyCompiler hints, Lombok-style processors
CLASSClass file, not at runtime (the default)Bytecode tools
RUNTIMEClass file and visible to reflectionFrameworks, 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).

Applying the annotation

java

Only createOrder carries the metadata. Annotations on a method are never inherited by overriding methods, so check the exact method you care about.

Reading the annotation with reflection

At runtime, Method.getAnnotation returns the annotation instance, or null when it is absent. This works only for RUNTIME retention.

java
text

Real code usually combines this with a dynamic proxy or an aspect, so the annotated method is intercepted automatically rather than scanned by hand.

Applying a custom annotation to fields

Annotations on fields are the basis of many small validation or mapping tools. This example checks that fields marked @NotBlankField are not empty:

java

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.

Compile-time processing

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).

Frequently Asked Questions

Why does getAnnotation return null?

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.

Can an annotation have inheritance or implement interfaces?

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.

Can I use the same annotation twice on one element?

Yes, if you mark it @Repeatable and provide a container annotation. Read the results with getAnnotationsByType.

Is reflection slow for this?

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.

Summary

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.

Share