Annotations in Java
Create custom metadata labels for your code using annotations, control their lifecycle with meta-annotations, and process them dynamically at runtime using the Reflection API.
We'll cover the following...
We have just learned how to use Reflection to inspect classes and methods from the outside. However, we often need to leave instructions inside the code itself. Annotations allow us to attach labels to methods and classes that describe intent without changing program execution. For example, we can tag a method to run before others or mark a class for database storage.
While Reflection provides the ability to inspect code structure, annotations provide the specific instructions that tell tools what that code is meant to do. This combination drives many modern Java frameworks, allowing us to write declarative code that handles complex tasks automatically.
Understanding metadata and built-in annotations
Annotations are metadata about data. They do not directly change how a method executes; instead, they provide information to the compiler or the runtime environment. We have already seen a few of these in earlier lessons without explicitly analyzing them.
To understand exactly how they work, we will look at Java’s three most common built-in annotations separately.
1. The compiler check: @Override
The @Override annotation guarantees that a method actually overrides a method from a superclass or interface. This is a safety mechanism. If we misspell the method name or get the parameters wrong, the compiler will catch the error immediately.
class Report {public void generate() {System.out.println("Generic Report");}}class PDFReport extends Report {// Without @Override, a typo like "generateReport" would compile// but would define a new method instead of overriding the parent.@Overridepublic void generate() {System.out.println("PDF Report");}}public class OverrideExample {public static void main(String[] args) {Report report = new PDFReport();report.generate();}}
Line 11: We place
@Overrideabove the method. If we accidentally typedpublic void generateReprt(), the compiler would stop with an error: “method does not override or implement a method from a supertype.”
2. The warning signal: @Deprecated
The @Deprecated annotation marks a class or method as obsolete. It tells other developers (and the compiler) that this code should no longer be used, likely because a better alternative exists. ...