Java - Java Annotations (Custom Annotations & Built-in Annotations) — Detailed Explanation
Java Annotations are a form of metadata that provide additional information about code but do not directly change program logic. They are used by the compiler, runtime, or development tools to process instructions, enforce rules, or generate code automatically.
Annotations help make code more readable, maintainable, and easier to manage, especially in large applications and frameworks like Spring and Hibernate.
What are Annotations in Java?
An annotation is a special kind of syntax that begins with the @ symbol and is placed above classes, methods, variables, or parameters.
Example:
@Override
public String toString() {
return "Example";
}
Here, @Override tells the compiler that the method is overriding a parent class method.
Purpose of Annotations
Annotations are used for:
-
Providing information to the compiler
-
Reducing boilerplate code
-
Enabling runtime processing
-
Supporting frameworks and tools
-
Improving code documentation and structure
Types of Annotations in Java
Java annotations are mainly divided into two categories:
1. Built-in Annotations
These are predefined annotations provided by Java.
a. @Override
Used when a method overrides a method from a parent class.
It helps catch errors at compile time.
b. @Deprecated
Marks a method or class as outdated and not recommended for use.
c. @SuppressWarnings
Instructs the compiler to ignore specific warnings.
Example:
@SuppressWarnings("unchecked")
d. @FunctionalInterface
Indicates that an interface has exactly one abstract method and can be used as a lambda expression.
2. Custom Annotations
Java allows developers to create their own annotations to define custom behavior.
How to Define a Custom Annotation
A custom annotation is created using the @interface keyword.
Example:
@interface Info {
String author();
int version();
}
Using Custom Annotation:
@Info(author = "Teena", version = 1)
public class Demo {
}
Here, Info is a custom annotation that stores metadata about the class.
Meta-Annotations in Java
Meta-annotations are annotations used to define other annotations.
Common meta-annotations include:
1. @Retention
Defines how long the annotation is retained:
-
SOURCE (discarded during compilation)
-
CLASS (stored in class file but not runtime)
-
RUNTIME (available at runtime)
2. @Target
Specifies where the annotation can be used:
-
METHOD
-
CLASS
-
FIELD
-
PARAMETER
3. @Inherited
Allows a subclass to inherit an annotation from its parent class.
4. @Documented
Ensures the annotation appears in JavaDoc documentation.
Real-World Usage of Annotations
Annotations are widely used in frameworks:
-
Spring Framework uses annotations like
@Autowired,@Component,@Service -
Hibernate uses
@Entity,@Table -
JUnit uses
@Testfor test methods
These annotations reduce configuration complexity and make development faster.
Advantages of Annotations
-
Reduces XML configuration
-
Improves code clarity
-
Supports automation in frameworks
-
Helps in compile-time and runtime checks
-
Makes code more structured and readable
Summary
Java Annotations are powerful metadata tools that help developers provide instructions to the compiler and frameworks without changing program logic. Built-in annotations handle common tasks, while custom annotations allow developers to define their own rules and behaviors. Meta-annotations extend their flexibility by controlling how and where annotations are used.
If you want, I can also show a real mini project example using annotations or explain how Spring Boot heavily depends on them.