How and where are Annotations used in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Annotations are a critical part of Java, introduced in Java 5, that provide metadata about the code elements such as classes, methods, and variables. They don't modify the action of the code they annotate but can be used by the compiler or application at runtime to perform specific actions or generate specific outcomes. Annotations can affect compilation, documentation generation, and runtime processing. This article explores how and where annotations are utilized in Java, while offering technical examples and explanations.
Types of Annotations
Annotations in Java can be broadly categorized into the following:
- Built-in Annotations: These annotations are provided by the Java standard library.
@Override: Indicates a method overrides a method from a superclass.@Deprecated: Denotes that a method, class, or field is deprecated and should not be used.@SuppressWarnings: Instructs the compiler to suppress specific warnings.
- Custom Annotations: These are user-defined according to the developer's specific requirements.
Technical Explanation and Usage
Built-in Annotations
1. @Override
This annotation ensures that a method is overriding a method from its superclass correctly. It helps prevent runtime errors.
In the example above, the @Override annotation ensures that SubClass is correctly implementing display as an overridden method.
2. @Deprecated
This annotation flags that an element is no longer recommended for use and may be removed in future versions.
Here, oldMethod is marked as deprecated, signaling developers that this method should be avoided.
3. @SuppressWarnings
Used to suppress compiler warnings.
In this example, the unchecked assignment warning is suppressed inside testMethod.
Custom Annotations
Custom annotations can be defined based on user requirements and can be targeted to specific sections of code.
In the example above, CustomAnnotation is defined with retention policy RUNTIME, meaning it will be available to the JVM at runtime.
Where Annotations are Used
Compilation
Some annotations instruct the compiler and can lead to generation of additional actions during the compilation phase.
Documentation
Annotations can be used to generate documentation. For instance, tools like Javadoc can read annotations and include related information in generated docs.
Runtime Processing
Annotations can be examined using reflection at runtime. This is especially useful in frameworks like Spring or Hibernate, where annotations control dependency injection or object-relational mapping.
In this example, reflection is used to process annotations present on methods at runtime.
Summary Table
| Annotation Type | Description | Example |
| @Override | Ensures method is overriden correctly from superclass | @Override public void methodName() { ... } |
| @Deprecated | Indicates an element is deprecated | @Deprecated void oldMethod() { ... } |
| @SuppressWarnings | Suppresses compiler warnings | @SuppressWarnings("unchecked") |
| Custom Annotation | User-defined annotations for custom metadata | @interface CustomAnnotation { ... }
@CustomAnnotation(date="...") |
Conclusion
Annotations in Java provide a robust way to add metadata to program elements and can influence code behavior during compile-time, documentation generation, and runtime. With built-in annotations improving code clarity and custom annotations allowing for tailored metadata usage, they serve as a powerful tool for Java developers seeking to enhance their applications' functionality and maintainability.

