Suppressing Warnings in Java

Banner with caution on it

You may have seen the @SuppressWarnings annotation before. Have you wondered how and why to use it?

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

That’s when we can use the @SuppressWarnings annotation to disable specific compiler warnings. You can use it to disable 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:

  • deprecation warns when you’re using a deprecated method or class.
  • unchecked tells 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.
  • rawtypes warns that you are using a raw type instead of a parameterized type. It is like unchecked, but it is used on fields and variables.
  • serial warns you about missing serialVersionUID definitions 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

For example, the following class generates four warnings: deprecationuncheckedrawtypes 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

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!

Sign up to get our weekly Java tips.

Leave a Comment

Your email address will not be published. Required fields are marked *