Java Annotations
What are Java Annotations?
Java Annotations are metadata added to code using the @ symbol. They provide additional information to the compiler, development tools, or runtime environments without directly affecting program logic.
Annotations are widely used for:
- Code documentation
- Compile-time error checking
- Runtime processing (via reflection)
Why Use Annotations?
Annotations improve code quality and maintainability by:
- Enhancing readability with clear developer intent
- Reducing errors through compile-time validation
- Supporting tools and frameworks (e.g., Spring, Hibernate)
- Simplifying configuration without extra code
Common Built-in Java Annotations
1. @Override Annotation
The @Override annotation ensures that a method correctly overrides a method from its superclass. It helps prevent subtle bugs caused by incorrect method signatures.
Example: Correct Method Overriding
class Vehicle {
void start() {
System.out.println("Vehicle is starting");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts with a key");
}
}
public class OverrideDemo {
public static void main(String[] args) {
Vehicle obj = new Car();
obj.start();
}
}
Why It Matters
If the method name or parameters are incorrect, the compiler will immediately flag an error—helping you avoid unexpected behavior.
2. @Deprecated Annotation
The @Deprecated annotation indicates that a class or method is outdated and may be removed in future versions. Developers are encouraged to use alternative approaches.
Example: Marking a Method as Deprecated
public class DeprecatedDemo {
@Deprecated
static void legacyLogin() {
System.out.println("Legacy login method");
}
static void modernLogin() {
System.out.println("Modern login method");
}
public static void main(String[] args) {
legacyLogin(); // Triggers a warning
modernLogin();
}
}
Key Insight
Although deprecated methods still work, IDEs and compilers will show warnings to guide developers toward better alternatives.
3. @SuppressWarnings Annotation
The @SuppressWarnings annotation instructs the compiler to ignore specific warnings, such as "unchecked" or "deprecation".
Example: Suppressing Unchecked Warning
import java.util.ArrayList;
public class SuppressWarningDemo {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// Using raw type (not recommended)
ArrayList rawList = new ArrayList();
rawList.add("Java");
rawList.add(100);
System.out.println(rawList);
}
}
Best Practice
Instead of suppressing warnings, it's better to fix them using generics:
import java.util.ArrayList;
public class SafeCollectionDemo {
public static void main(String[] args) {
ArrayList<String> safeList = new ArrayList<>();
safeList.add("Java");
System.out.println(safeList);
}
}
Custom Annotations (Advanced Concept)
Java also allows you to create your own annotations using @interface.
Example: Creating a Custom Annotation
@interface Author {
String name();
int version();
}
@Author(name = "Kavi", version = 1)
class ProjectModule {
void display() {
System.out.println("Custom annotation example");
}
}
Custom annotations are commonly used in frameworks for configuration and dependency injection.
Key Advantages of Java Annotations
- Improve code clarity and documentation
- Enable compile-time validation
- Support automation and frameworks
- Reduce boilerplate configuration code