




- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


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:
- Better readability and less clutter
- Compile-time error checking
- Cleaner and more maintainable code
- Support for tools and frameworks that process metadata
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:
@Target
– Specifies the kinds of elements an annotation can be applied to.@Retention
– Indicates how long annotations are retained (SOURCE, CLASS, or RUNTIME).@Documented
– Marks an annotation for inclusion in the generated JavaDoc.@Inherited
– Allows subclasses to inherit the annotation from a superclass.
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:
- Spring Framework –
@Autowired
,@Component
,@Service
- Hibernate –
@Entity
,@Table
,@Id
- JUnit –
@Test
,@Before
,@After
Summary
- Annotations are a way to add metadata to Java code.
- They improve readability and tool support.
- You can create your own annotations and use reflection to act on them.