Java : @SuppressWarnings (annotation) with Examples

SuppressWarnings (Java SE 20 & JDK 20) with Examples.
You will find code examples on most SuppressWarnings methods.


Summary

Indicates the warnings to be suppressed at compile time in the annotated element, and in all elements contained in the annotated element.

Class diagram

Four kinds of warnings that can be specified by @SuppressWarnings:

Please see also Java Language Specification :


unchecked

An example of a warning :

public class Foo {
    // This code intentionally omit the type parameter E for List<E>.
    public void m(List list) {
        list.add("aaa");
    }
}

//> javac -Xlint:unchecked Foo.java
//Foo.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
//        list.add("aaa");
//                ^
//  where E is a type-variable:
//    E extends Object declared in interface List
//1 warning

An example of suppressing warnings :

public class Bar {
    @SuppressWarnings("unchecked")
    public void m(List list) {
        list.add("aaa");
    }
}

//> javac -Xlint:unchecked Bar.java
// <No errors and warnings>

deprecation

An example of a warning :

@Deprecated
public class Sample {
}
public class Foo {
    public void m() {
        final var sample = new Sample();
        System.out.println(sample);
    }
}

//> javac -Xlint:deprecation Foo.java
//Foo.java:5: warning: [deprecation] Sample in unnamed package has been deprecated
//        final var sample = new Sample();
//                               ^
//1 warning

An example of suppressing warnings :

public class Bar {
    @SuppressWarnings("deprecation")
    public void m() {
        final var sample = new Sample();
        System.out.println(sample);
    }
}

//> javac -Xlint:deprecation Bar.java
// <No errors and warnings>

removal

An example of a warning :

@Deprecated(forRemoval = true)
public class Sample {
}
public class Foo {
    public void m() {
        final var sample = new Sample();
        System.out.println(sample);
    }
}

//> javac Foo.java
//Foo.java:5: warning: [removal] Sample in unnamed package has been deprecated and marked for removal
//        final var sample = new Sample();
//                               ^
//1 warning

An example of suppressing warnings :

public class Bar {
    @SuppressWarnings("removal")
    public void m() {
        final var sample = new Sample();
        System.out.println(sample);
    }
}

//> javac Bar.java
// <No errors and warnings>

preview

An example of a warning :

public class Foo {
    public void m() {
        final var t = Thread.ofVirtual();
        System.out.println(t);
    }
}

//> javac --enable-preview --release 20 -Xlint:preview Foo.java
//Foo.java:5: warning: [preview] ofVirtual() is a preview API and may be removed in a future release.
//        final var t = Thread.ofVirtual();
//                            ^
//1 warning

An example of suppressing warnings :

public class Bar {
    @SuppressWarnings("preview")
    public void m() {
        final var t = Thread.ofVirtual();
        System.out.println(t);
    }
}

//> javac --enable-preview --release 20 -Xlint:preview Bar.java
// <No errors and warnings>

Related posts

To top of page