Comprehensive Guide to Java Annotations (Detailed)
This article provides an in‑depth overview of Java annotations, covering their definition, common applications, built‑in standard annotations, meta‑annotations, functional interfaces, and step‑by‑step instructions for creating and using custom annotations with complete code examples.
Java annotations, introduced in JDK 5, are a form of metadata that can be attached to packages, classes, interfaces, fields, methods, parameters, and local variables to convey additional information to tools at compile‑time or runtime.
Annotations serve several purposes: generating documentation, performing compile‑time checks (e.g., @Override ), configuring frameworks (e.g., Spring), and enabling reflection‑based processing.
Types of annotations
Standard annotations provided by the JDK such as @Override , @Deprecated , and @SuppressWarnings .
Meta‑annotations that define other annotations, including @Retention , @Target , @Documented , @Inherited , and @Repeatable .
Custom annotations created by developers for specific needs.
Standard annotations examples
class Parent { public void test() {} } class Child extends Parent { // Uncomment the following to see a compile‑time warning // @Override // public void test() {} }
@Deprecated public class TestClass { // ... }
@SuppressWarnings("unchecked") public void addItems(String item) { List items = new ArrayList(); items.add(item); }
Variants of @SuppressWarnings can suppress single, multiple, or all warning types by adjusting the annotation parameters.
Functional Interface
Since Java 8, the @FunctionalInterface annotation marks an interface as having exactly one abstract method, enabling lambda expressions.
@FunctionalInterface public interface UserService { void getUser(Long userId); default void setUser() {} static void saveUser() {} boolean equals(Object obj); }
Meta‑annotations
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { }
The RetentionPolicy enum defines where the annotation is retained (SOURCE, CLASS, RUNTIME). The ElementType enum defines the valid program elements for an annotation (TYPE, FIELD, METHOD, etc.).
Other meta‑annotations include:
@Documented – included in Javadoc.
@Inherited – inherited by subclasses.
@Repeatable – allows the same annotation to be applied multiple times.
Creating a custom annotation
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited public @interface HelloAnnotation { String value(); }
Usage example:
public class HelloAnnotationClient { @HelloAnnotation(value = "Simple custom Annotation example") public void sayHello() { System.out.println("Inside sayHello method.."); } }
Testing the custom annotation via reflection:
public class HelloAnnotationTest { public static void main(String[] args) throws Exception { HelloAnnotationClient client = new HelloAnnotationClient(); Method method = client.getClass().getMethod("sayHello"); if (method.isAnnotationPresent(HelloAnnotation.class)) { HelloAnnotation ann = method.getAnnotation(HelloAnnotation.class); System.out.println("Value : " + ann.value()); method.invoke(client); } } }
The article concludes with additional resources and promotional links for further learning about JVM, concurrency, SSO, message queues, and career development.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.