Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Annotations



Java Annotations are one of those features that quietly power many of Java’s most powerful frameworks—like Spring, Hibernate, and JUnit—yet they often get misunderstood or overlooked by beginners. In this tutorial, we’ll explore what annotations are, why they exist, and how to use them effectively in your programs.

What are Java Annotations?

Annotations in Java are a special kind of metadata that provide information about the code but do not directly affect its execution. They act like notes to the compiler or runtime, guiding them on how to treat the annotated elements (classes, methods, variables, etc.).

Why Use Annotations?

Before annotations, developers relied heavily on XML configuration files or marker interfaces. Annotations offer:

Built-in Annotations in Java

Java provides several built-in annotations, especially useful for compile-time checks.

@Override

Indicates that a method is intended to override a method in a superclass.

class Animal {
    void speak() {
        System.out.println("Animal speaks");
    }
}

class Dog extends Animal {
    @Override
    void speak() {
        System.out.println("Dog barks");
    }
}
Dog barks

@Deprecated

Marks a method or class as outdated, warning developers to avoid using it.

class LegacyCode {
    @Deprecated
    void oldMethod() {
        System.out.println("Old method, avoid using.");
    }
}

@SuppressWarnings

Instructs the compiler to suppress specific warnings.

@SuppressWarnings("unchecked")
List list = new ArrayList();

Meta-Annotations

Java also provides meta-annotations that annotate other annotations:

Creating Custom Annotations

Let’s see how you can define your own annotation:

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface LogExecutionTime {
}

Now, apply this annotation to a method:

class Demo {
    @LogExecutionTime
    void run() {
        System.out.println("Running method...");
    }
}

Accessing Annotations at Runtime (Reflection)

Using Java Reflection API, we can process annotations at runtime.

import java.lang.reflect.Method;

class AnnotationProcessor {
    public static void main(String[] args) throws Exception {
        Demo d = new Demo();
        Method m = d.getClass().getMethod("run");

        if (m.isAnnotationPresent(LogExecutionTime.class)) {
            long start = System.currentTimeMillis();
            m.invoke(d);
            long end = System.currentTimeMillis();
            System.out.println("Execution time: " + (end - start) + "ms");
        }
    }
}
Running method...
Execution time: Xms

Real-World Use Cases

Annotations are not just academic—they are used widely in:

Summary

QUIZ

Question 1:What is the primary purpose of annotations in Java?

Question 2:Java annotations can be applied to classes, methods, and variables.

Question 3:Which of the following is a correct use of the @Override annotation?

Question 4:Which of the following are valid meta-annotations in Java?

Question 5:The @SuppressWarnings annotation changes how the JVM executes the code.

Question 6:Which of the following statements about custom annotations and reflection in Java are true?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M