Nearby lessons
107 of 125Java - Annotations
- What annotations are and why they are useful
- The built-in annotations: @Override, @Deprecated, @SuppressWarnings
- Meta-annotations: @Retention and @Target
- Creating your own custom annotation
Annotations is a core concept of the Java language. This lesson explains What is an Annotation?, Important Built-in Annotations and Meta-Annotations — Annotations About Annotations with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is an Annotation?
An annotation is a small piece of information (metadata) attached to code — like a sticky note on a class, method or variable. It does not change how the code runs by itself; instead, tools and frameworks read it and act on it.
Think of annotations as labels on boxes in a warehouse: 'FRAGILE' (handle carefully), 'EXPIRED' (throw away). The box does not change — but the workers (tools) behave differently when they see the label.
Important Built-in Annotations
| Annotation | Used on | What it tells |
|---|---|---|
| @Override | Methods | "I am redefining a parent method." Compiler warns if the signature is wrong. |
| @Deprecated | Classes/methods | "This is old — avoid using it." Compiler warns users. |
| @SuppressWarnings("unchecked") | Methods/classes | "Stop showing these warnings." |
| @FunctionalInterface | Interfaces | "This interface has exactly one abstract method (can be a lambda)." |
| @SafeVarargs | Methods | "These var-args are safe." |
Meta-Annotations — Annotations About Annotations
When we create our own annotation, we must decorate it with meta-annotations that describe where it works and how long it lives:
| Meta-annotation | What it controls |
|---|---|
| @Retention | How long the annotation is kept: SOURCE, CLASS (compile), or RUNTIME (needed for reflection). |
| @Target | Where it can be used: METHOD, FIELD, CLASS, PARAMETER, etc. |
| @Documented | The annotation will appear in javadoc. |
| @Inherited | Child classes also get this annotation. |
Creating a Custom Annotation
Real-World Use of Annotations
Annotations are everywhere in modern Java. Here is where you meet them daily:
| Framework / API | Famous annotations |
|---|---|
| JUnit (testing) | @Test, @BeforeEach, @AfterEach |
| Spring (web apps) | @Component, @RestController, @Autowired |
| Spring Boot | @SpringBootApplication |
| Hibernate (database) | @Entity, @Table, @Column |
| JDK (Java 8+) | @FunctionalInterface, @Deprecated |
The Two Big Questions About Annotations
Q1 — We already have comments; why annotations?
Because comments are removed during compilation (the lexical analysis phase deletes them). So comments exist only in the .java file — they are gone from the .class file and unavailable at run time. Annotations survive compilation and can be read by your program at run time (with reflection).
| Point | Comments | Annotations |
|---|---|---|
| Present after compilation? | No (removed) | Yes (if RetentionPolicy.RUNTIME) |
| Readable by the program? | No | Yes |
| Best for | Human descriptions | Metadata for tools and frameworks |
Q2 — We already have XML; why annotations?
XML configuration was the old way, but it came with problems: you had to learn XML, check that files were in the right place and correctly formatted, and write parsing code. Annotations are Java-native and remove all of that.
| Point | XML documents | Annotations |
|---|---|---|
| Extra technology to learn? | Yes (XML + parsers) | No |
| File placement/format checks | Needed every time | Not needed |
| Modern frameworks | Old style (up to Java 1.4-era) | Modern default (Spring Boot, JUnit 5) |
Annotations vs Comments vs XML
A very good question from the classic material: we already have comments to describe code, so why do we need Annotations (Chapter 20)?
The problem: when you compile a program, the lexical analysis phase of the compiler removes comments. So comments exist only in the .java file — they are gone from the .class file and unavailable at run time.
Annotations are different — they stay with the code even after compilation, up to run time, and can be read programmatically (with reflection).
| Point | Comments | Annotations | XML documents |
|---|---|---|---|
| Available after compilation? | No (removed) | Yes | Yes (separate file) |
| Readable by your program? | No | Yes | Yes |
| Extra learning needed? | No | No | Yes (XML + parsing) |
| Best for | Human descriptions | Metadata for tools/frameworks | Configuration (older style) |
- Annotations are metadata — sticky notes on code; they do not change execution by themselves.
- @Override, @Deprecated, @SuppressWarnings are the common built-in ones.
- @Retention decides how long the annotation lives; @Target decides where it can be used.
- Custom annotations are declared with @interface and read using reflection.
- Frameworks like Spring, JUnit and Hibernate are powered by annotations + reflection.