Annotations in Java
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.
What an annotation is
Metadata attached to a class, method, or field with an @ prefix, readable by the compiler, a tool, or a framework.
@Override
Has the compiler verify a matching superclass method genuinely exists to override, catching a typo as a compile error.
@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 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.
public class AnnotationDemo { @Deprecated static void oldMethod() { System.out.println("This method is outdated"); } public static void main(String[] args) { oldMethod(); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.