Annotation in Java is a special type of metadata that provides additional information about classes, methods, variables, constructors, or other program elements.
In simple words:
Annotations tell the compiler, JVM, or frameworks how the code should behave without changing actual business logic.
Why Annotations are Important?
Annotations are widely used for:
- Framework configuration
- Dependency Injection
- Spring Boot development
- Hibernate ORM mapping
- REST API development
- Testing frameworks
- Code generation
- Validation
Annotation Overview Diagram
Java Code
|
v
Annotations Added
|
v
Compiler / JVM / Framework Reads Metadata
|
v
Special Behavior Applied
Basic Annotation Example
@Override
public String toString() {
return "Employee";
}
What is @Override?
@Override tells compiler:
"This method is overriding parent class method."
Annotation Syntax
@AnnotationName
Example
@Entity @Service @RestController @Test
Where Can Annotations Be Applied?
- Classes
- Methods
- Fields
- Constructors
- Parameters
- Interfaces
- Packages
Annotation Processing Flow
Annotation Added
|
v
Compiler/JVM/Framework Detects Annotation
|
v
Metadata Processed
|
v
Special Logic Applied
Types of Annotations in Java
- Built-in Annotations
- Meta Annotations
- Custom/User-Defined Annotations
1. Built-in Annotations
Java provides predefined annotations.
Common Built-in Annotations
- @Override
- @Deprecated
- @SuppressWarnings
- @FunctionalInterface
@Override Example
class Parent {
void show() {
}
}
class Child
extends Parent {
@Override
void show() {
}
}
Why Important?
Compiler verifies correct method overriding.
@Override Flow
Method Declared
|
v
@Override Found
|
v
Compiler Checks Parent Method
|
v
Validation Successful
@Deprecated Annotation
Marks old or outdated methods/classes.
Example
@Deprecated
void oldMethod() {
}
Meaning
Developers should avoid using deprecated code.
@SuppressWarnings Annotation
Suppresses compiler warnings.
Example
@SuppressWarnings("unchecked")
@FunctionalInterface Annotation
Indicates interface contains only one abstract method.
Example
@FunctionalInterface
interface Test {
void show();
}
2. Meta Annotations
Meta annotations are annotations applied to other annotations.
Common Meta Annotations
- @Target
- @Retention
- @Inherited
- @Documented
- @Repeatable
@Target Annotation
Specifies where annotation can be used.
Example
@Target(ElementType.METHOD)
Meaning
Annotation can only be applied to methods.
@Retention Annotation
Defines how long annotation is available.
Retention Policies
| Policy | Meaning |
|---|---|
| SOURCE | Available only in source code |
| CLASS | Stored in .class file |
| RUNTIME | Available during runtime |
Example
@Retention(
RetentionPolicy.RUNTIME
)
Why Runtime Retention Important?
Reflection API can access annotation during runtime.
3. Custom Annotation
Developers can create their own annotations.
Custom Annotation Example
@interface Author {
String name();
}
Usage Example
@Author(name = "Naresh")
class Employee {
}
Custom Annotation Flow
Custom Annotation Created
|
v
Applied to Class/Method
|
v
Reflection Reads Annotation
|
v
Custom Logic Executed
Annotations and Reflection API
Reflection is used to read annotations dynamically.
Example
if(
c.isAnnotationPresent(
Author.class
)
) {
}
Annotations in Spring Boot
Spring Boot heavily uses annotations.
Common Spring Annotations
- @SpringBootApplication
- @RestController
- @Service
- @Repository
- @Autowired
- @Component
- @RequestMapping
Spring Boot Example
@RestController
class UserController {
@Autowired
UserService service;
}
What Spring Does Internally?
- Scans annotations
- Creates beans dynamically
- Injects dependencies
- Configures REST APIs
Spring Annotation Flow
Application Starts
|
v
Classpath Scanned
|
v
Annotations Found
|
v
Reflection Processes Metadata
|
v
Beans Created Automatically
Annotations in Hibernate/JPA
Hibernate uses annotations for ORM mapping.
Example
@Entity
@Table(name="employees")
class Employee {
@Id
private int id;
}
What Happens?
- Reflection reads entity metadata
- Database tables mapped automatically
Hibernate Flow
@Entity Found
|
v
Reflection Reads Fields
|
v
Database Mapping Created
Annotations in JUnit
JUnit uses annotations for test execution.
Example
@Test
public void testLogin() {
}
JUnit Flow
@Test Found
|
v
Reflection Invokes Method
|
v
Test Executed
Difference Between Annotation and Interface
| Feature | Annotation | Interface |
|---|---|---|
| Purpose | Metadata | Abstraction |
| Contains Logic? | No | Yes |
| Runtime Processing | Yes | No |
| Used By | Frameworks/JVM | Application Design |
Advantages of Annotations
- Reduces XML configuration
- Simplifies framework development
- Improves readability
- Supports automation
- Enables runtime metadata processing
Disadvantages of Annotations
- Overuse reduces readability
- Hidden framework magic
- Debugging can become complex
Annotations in Banking Systems
Banking systems use annotations for:
- Transaction management
- Security rules
- Audit logging
- REST APIs
Banking Example
@Transactional
public void transferMoney() {
}
What Happens?
Spring automatically manages transaction boundaries.
Annotations in E-Commerce Systems
E-commerce platforms use annotations for:
- Product APIs
- Validation
- Database mapping
- Payment processing
Annotations in Microservices
Microservices architectures heavily use annotations for:
- REST APIs
- Service discovery
- Distributed tracing
- Security
- Configuration management
Microservice Annotation Flow
@RestController
|
v
@RequestMapping
|
v
REST Endpoint Created Automatically
Annotations and JVM
Annotations are stored as metadata in class files and accessed using Reflection API.
JVM Annotation Architecture
Java Source Code
|
v
Annotations Compiled into Metadata
|
v
JVM Loads Metadata
|
v
Reflection API Reads Metadata
Common Interview Mistake
Many developers think annotations directly execute logic.
Actually:
- Frameworks and JVM read annotations and apply behavior.
Another Common Mistake
Many developers think annotations replace business logic.
Actually:
- Annotations only provide metadata.
Best Practices
- Use annotations meaningfully
- Avoid excessive custom annotations
- Document annotation behavior clearly
- Prefer runtime annotations only when needed
- Keep annotation-driven logic maintainable
Realtime Enterprise Example
Spring REST API
@RestController Added
|
v
Spring Detects Annotation
|
v
REST Endpoint Registered
|
v
API Available Automatically
Related Learning Topics
- What is Reflection API in Java
- What is Java Reflection Used For
- What is ClassLoader in Java
- How JVM Works Internally
- What is Serialization in Java
- Memory Management in Java
- What is Spring Boot
- What are Microservices
Professional Interview Answer
Annotation in Java is a metadata mechanism used to provide additional information about classes, methods, fields, constructors, and other program elements without changing actual business logic. Annotations are heavily used by compilers, JVM, and frameworks like Spring Boot, Hibernate, JUnit, and modern microservices platforms to enable dependency injection, ORM mapping, REST API configuration, transaction management, validation, testing, and runtime processing. Java supports built-in annotations, meta annotations, and custom annotations, and annotations are typically processed using Reflection API during runtime. Annotations significantly simplify enterprise application development by reducing XML configuration and enabling powerful framework automation.
Frequently Asked Questions
What is annotation in Java?
Annotation is metadata that provides additional information about Java code.
Which package is used for annotation processing?
Reflection API from java.lang.reflect is commonly used.
What is the purpose of @Override?
It tells compiler that method overrides parent class method.
Can we create custom annotations?
Yes, Java allows creation of custom annotations.
Which frameworks heavily use annotations?
Spring Boot, Hibernate, and JUnit heavily use annotations.