Learn Java
import com.learnjava.advancedjava.Annotations;

Annotations in Java

@Concept

An annotation, written with an @ prefix like @Override or @Deprecated, attaches metadata to a class, method, or field — information that doesn't change what the code actually does at runtime by itself, but that the compiler, a tool, or a framework can read and act on. @Override, seen already, has the compiler verify a matching superclass method genuinely exists; @Deprecated marks something as no longer recommended, triggering a compiler warning at every call site still using it; @SuppressWarnings tells the compiler to suppress a specific category of warning that would otherwise be reported for the annotated code.

Custom annotations can also be defined, using @interface, and given their own retention policy — whether the annotation should be discarded after compiling, kept in the compiled class file but ignored at runtime, or actually kept and readable through reflection while the program runs. That runtime-readable case is exactly how frameworks like Spring implement annotations such as @Autowired: the framework uses reflection at startup to scan for that specific annotation and wire up dependencies accordingly, all driven by metadata rather than by any code the annotated class itself contains.

@HowItWorks
1

What an annotation is

Metadata attached to a class, method, or field with an @ prefix, readable by the compiler, a tool, or a framework.

2

@Override

Has the compiler verify a matching superclass method genuinely exists to override, catching a typo as a compile error.

3

@Deprecated

Marks something as no longer recommended, triggering a compiler warning at every call site still using it.

4

@SuppressWarnings

Tells the compiler to suppress a specific category of warning that would otherwise be reported for the annotated code.

5

Custom annotations and retention

Defined with @interface, and given a retention policy controlling whether they survive into the compiled class file or stay readable at runtime through reflection.

@Example
public class AnnotationDemo {
@Deprecated
static void oldMethod() {
System.out.println("This method is outdated");
}
 
public static void main(String[] args) {
oldMethod();
}
}
Output
This method is outdated
1@Deprecated marks oldMethod as no longer recommended, without changing anything about how it actually runs.
2Calling oldMethod() still works exactly as written; the annotation only affects compiler warnings, not runtime behavior.
3A real compiler would report a deprecation warning at this call site, though the program still compiles and runs normally.
4The single println call inside oldMethod runs as usual, printing its one line of output.
@UseCases
Catching a typo in an overriding method's name at compile time with @Override
Warning other developers away from a method that's being phased out, with @Deprecated
Building a framework that uses reflection to discover and act on custom annotations at runtime
Suppressing a specific, already-understood compiler warning for one method with @SuppressWarnings

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.