The @SuppressWarnings annotation type allows Java programmers to disable compilation warnings for a certain part of a program (type, field, method, parameter, constructor, and local variable). Normally warnings are good. However in some cases they would be inappropriate and annoying. So programmers can choose to tell the compiler ignoring such warnings if needed.

This annotation type must be used with one or more warnings as its arguments, for example:

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "deprecation"})

List of warnings vary among Java compilers. The Java Language Specification mentions only “unchecked” warning (section 9.6.3.5). To see the list of warnings which would be issued by the Oracle’s Java compiler, type the following command:

javac -X

We’ll see the complete list of warnings after the line starts with -Xlint:{, as shown in the following screenshot:

list of warnings by Oracle Java compiler

So the warnings issued by Oracle’s Java compiler (as of Java 1.7) are: all, cast, classfile, deprecation, dep-ann, divzero, empty, fallthrough, finally, options, overrides, path, processing, rawtypes, serial, static, try, unchecked, varargs.

Different IDEs provide different list of warnings issued by their own compilers, e.g. list of suppress warnings provided by Eclipse IDE’s Java compiler.

Here are some of the most commonly used warnings which have same meaning across compilers: all, deprecation, unchecked.

 

Java @SuppressWarnings Examples:

  • Suppressing warnings on using unchecked generic types operations:
    @SuppressWarnings("unchecked")
    void uncheckedGenerics() {
    	List words = new ArrayList();
    
    	words.add("hello");	// this causes unchecked warning
    }

    If the above code is compiled without @SuppressWarnings("unchecked")annotation, the compiler will complain like this:

    XYZ.java uses unchecked or unsafe operations.
  • Suppressing warnings on using deprecated APIs:
    @SuppressWarnings("deprecation")
    public void showDialog() {
    	JDialog dialog = new JDialog();
    	dialog.show();	// this is a deprecated method
    } 
  • Without the @SuppressWarnings("deprecation") annotation, the compiler will issue this warning:

    XYZ.java uses or overrides a deprecated API.
     
  • Suppressing multiple warnings:

    Suppress all unchecked and deprecation warnings for all code inside the Foo class below:

    @SuppressWarnings({"unchecked", "deprecation"})
    class Foo {
    	// code that may issue unchecked and deprecation warnings
    } 
  • Suppressing warnings on local variable declaration:
    void foo(List inputList) {
    	@SuppressWarnings("unchecked")
    	List<String> list = (List<String>) inputList; // unsafe cast
    } 

 

Some Notes about @SuppressWarnings:

    • Undefined warnings have no effect, e.g. @SuppressWarnings(“blahblah”). The compiler will ignore that silently.
    • When a program’s element is annotated by the @SuppressWarnings, all of its sub elements are also affected. For example, if you suppress a warning at class level, then all code inside that class is also applied.
    • It’s recommend to this annotation on the most deeply nested element wherever possible.

       

 

Other Annotations in Core Java:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#6Garden Craft Marker2020-10-29 04:45
This annotation can be used to turn off compiler warnings - either all warnings or only certain . A clean-room implementation of the findbugs annotations released under the Apache License, version 2.0 - stephenc/findbugs-annotations.You should only use @SuppressWarnings annotation or the assert statement.
Quote
#5Chornge2017-06-28 13:00
sorry, that was an error, ignore comment #4.
Quote
#4Chornge2017-06-28 12:54
first, subtract sqrt(x) from both sides.
then, square both sides.
: x + 15 = 225 -15x+x
then, collect like terms.
//if everything has been done correctly, you are left with:
17x = 210
finally, divide both sides by 17.
x = 30.
Quote
#3Nam2016-05-11 10:33
Quoting SRINIVASA RAO KARRA:
Please send me free java tutorial videos to my email Id


So you register to get the videos here: newsletter.codejava.net
Quote
#2SRINIVASA RAO KARRA2016-05-10 01:53
Please send me free java tutorial videos to my email Id
Quote