Backend Development 7 min read

What Are Annotations in Java? Definition, Uses, Meta‑Annotations, and Custom Annotation Example

This article explains Java annotations—what they are, how they work, their common uses such as documentation generation and compile‑time checks, the four built‑in meta‑annotations, standard annotations like @Override, and how to create and apply custom annotations with a complete code example.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
What Are Annotations in Java? Definition, Uses, Meta‑Annotations, and Custom Annotation Example

Annotations, introduced in Java 5, are a language feature that allows developers to attach metadata to program elements such as classes, methods, fields, parameters, and local variables. The metadata is stored in the compiled bytecode and can be accessed by tools or frameworks at compile time or runtime via reflection.

Annotations serve several purposes: generating documentation (e.g., @param, @return), providing configuration information that can replace external files (e.g., Dagger 2 dependency injection), and enabling compile‑time checks such as ensuring a method truly overrides a superclass method with @Override.

The underlying mechanism is that an annotation type is a special interface that extends java.lang.annotation.Annotation . At runtime the JVM creates a dynamic proxy instance for each annotation, and the proxy’s invoke method retrieves values from a map populated from the class file’s constant pool.

Java defines four meta‑annotations that are used when creating custom annotations:

@Documented – indicates the annotation should be included in Javadoc.

@Retention – specifies the annotation’s lifecycle (SOURCE, CLASS, or RUNTIME).

@Target – restricts where the annotation can be applied (e.g., CONSTRUCTOR, FIELD, METHOD, TYPE, etc.).

@Inherited – allows the annotation to be inherited by subclasses.

Common built‑in annotations include:

@Override – asserts that a method overrides a superclass method; a compilation error occurs otherwise.

@Deprecated – marks a program element as obsolete, causing the compiler to generate warnings when it is used.

@SuppressWarnings – takes a String[] argument to silence specific compiler warnings (e.g., @SuppressWarnings("unchecked") ).

When defining a custom annotation, the following rules apply:

Use @interface to declare the annotation; it implicitly extends Annotation .

Members may be public or have default (package‑private) visibility.

Member types are limited to primitive types, String , Class , Enum , other annotations, or arrays of these.

Access to annotation values at runtime requires reflection.

A custom annotation can have no members, but then it provides little value.

The article provides a complete example of a custom annotation system for describing fruit information, including source files FruitName.java , FruitColor.java , FruitProvider.java , FruitInfoUtil.java , Apple.java , and FruitRun.java . Running the example produces output such as:

Fruit name: Apple Fruit color: RED Supplier ID: 1 Supplier name: Shaanxi Hongfushi Group Supplier address: No. 89 Yan’an Road, Xi’an, Shaanxi

References are provided for further reading.

JavaReflectionCustom AnnotationannotationsMeta‑annotations
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.