You may have seen the @SuppressWarnings annotation before. Have you wondered how and why to suppress warnings in Java?
Errors and warnings
The Java compiler is very strict. It generates errors and stops compiling if there’s something seriously wrong with your code. However, sometimes it just warns you of potential problems.
Compiler warning messages are usually helpful. But sometimes warnings can be noisy, especially when you can’t or don’t want to address them.
For example, the compiler will warn you if you use a deprecated class or method. Re-compiling with the -Xlint or the more specific -Xlint:deprecation flag will give you some extra information. But what happens if you can’t or don’t want to re-write the offending code, but do want to get rid of the warning?
Warning types
You can decide to suppress warnings. The purpose of the @SuppressWarnings annotation to disable specific compiler warnings. You can use it to disable or suppress warnings for a specific part of a class. It can be used on types, fields, constructors, methods, parameters and local variables. It allows you to specify which warnings you’d like the compiler to ignore. The annotation takes a single String[] which you use to specify all the warnings you’d like to ignore.
Warning types vary from compiler to compiler, but a few of the most common include:
deprecationwarns when you’re using a deprecated method or class.uncheckedtells you when you’re using raw types instead of parameterized types. An unchecked warning says that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with@SuppressWarnings("unchecked")tells the compiler that you believe the code is safe and won’t cause unexpected exceptions.rawtypeswarns that you are using a raw type instead of a parameterized type. It is likeunchecked, but it is used on fields and variables.serialwarns you about missingserialVersionUIDdefinitions on serializable classes.
To get a list of the warnings that the compiler you’re using can generate, run javac -X on the command line.
Example of how to suppress warnings
For example, the following class generates four warnings: deprecation, unchecked, rawtypes and serial
public class ClassWithLotsOfWarnings implements Serializable {
// no serialVersionUID field
// a raw type
private List list;
public static void main(String args[]) {
// deprecated constructor
Date date = new Date (100, 11, 07);
System.out.println(date);
}
public void add(String str) {
// unchecked operation
list.add(str);
}
} // end of class
To suppress all the warnings, you could do the following:
@SuppressWarnings({"serial", "deprecation",
"rawtypes", "unchecked"})
public class ClassWithLotsOfWarnings implements Serializable {
...
} // end of class
Best practice for suppressing warnings
The best practice is to annotate the closest element to where you need to suppress the warning. For example, if you want to suppress a warning in a specific method, you should annotate the method, not the class.
To suppress the warnings at the most appropriate places in the code, you could do the following:
@SuppressWarnings("serial")
public class ClassWithLotsOfWarnings implements Serializable {
@SuppressWarnings("rawtypes")
private List list;
@SuppressWarnings("deprecation")
public static void main(String args[]) {
Date date = new Date (100, 11, 07);
System.out.println(date);
}
@SuppressWarnings("unchecked")
public void add(String str) {
list.add(str);
}
} // end of class
Now you know how to use @SuppressWarnings!
I’m always interested in your opinion, so please leave a comment. Your feedback helps me write tips that help you.