Java Standard Annotations

Among annotations, annotations provided by the Java SE standard are called standard annotations.

As of Java SE 8, the following standard annotations are available.

@Override Annotation: Explicit Override

Declares that the method overrides a method of the superclass. Omitting it does not affect the behavior itself, but by making it explicit, the compiler raises a warning if there is a typo in the method name or arguments.

Example) The following is an example of the @Override annotation.

public class Animal {
    public void speak() {
    }

    public String getType() {
        return "Generic animal";
    }
}

public class Cat extends Animal {
    @Override
    public void speak() { // This is a good override.
        System.out.println("Meow.");
    }

    @Override
    public String gettype() { // Compile-time error due to mistyped name.
        return "Cat";
    }
}

@Deprecated Annotation: Declared on Classes or Methods No Longer Used

Declares that a method, class, or method is deprecated and should no longer be used. Code that uses it generates a warning at compile time.

@Deprecated
public void foo() { ... }

@SuppressWarnings Annotation: Suppressing Warning Messages

Suppresses specific warnings generated by the compiler. For example, when intentionally using deprecated methods for reasons such as backward compatibility, repeated warnings can be distracting. In such cases, this annotation can be declared to explicitly turn off warnings.

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    ClassSample g = new ClassSample();
    g.hoge(); // deprecated foo method
}

Other settings that can be used with the @SuppressWarnings annotation are as follows.

Default settings for the @SuppressWarnings annotation

Setting Value Warning Content
cast Unnecessary cast
deprecation Use of a deprecated member
divzero Division by zero
unused There is an unused variable

References