Memra

Built-in annotations: @Override, @Deprecated, @SuppressWarnings and friends

The five standard annotations and exactly what each one tells the compiler.

What annotations are

Annotations are metadata — they attach information to a class, method, field, or parameter. They don't change runtime behaviour by themselves; the compiler, build tools, or frameworks read them and act accordingly.

Five built-in annotations appear on the OCP exam:

public class MyList<T> {

    @Override
    public String toString() { return "MyList"; }

    @Deprecated
    public void oldMethod() { /* ... */ }

    @SuppressWarnings("unchecked")
    public void mixedList() {
        List raw = new ArrayList();  // normally a compiler warning
    }

    @SafeVarargs
    @SuppressWarnings("varargs")
    public final void safe(T... items) { /* ... */ }
}

@FunctionalInterface
interface Transformer<T, R> {
    R transform(T input);  // exactly one abstract method
}

| Annotation | Applied to | Effect | |---|---|---| | @Override | Methods | Compiler error if the method doesn't actually override a superclass method or implement an interface method | | @Deprecated | Almost anything | Compiler warning at call sites; signals the element is scheduled for removal | | @SuppressWarnings | Most elements | Silences named compiler warnings — "unchecked", "rawtypes", "deprecation", etc. | | @FunctionalInterface | Interfaces | Compile error if the interface has more or fewer than one abstract method | | @SafeVarargs | final/static/private methods with varargs | Suppresses the heap-pollution warning for generic varargs |

@Override is the most important habit: it catches silent bugs where a typo means you defined a *new* method rather than overriding an existing one.

NORMAL ~/memra/learn/java-from-zero/built-in-annotations utf-8 LF