When to suppress warnings in Java

Caution - when to suppress warnings in Java

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:

  • 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 of how to suppress warnings

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 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.

Leave a Comment

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

Code like a Java Guru!

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email info@incusdata.com

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.

Your Java tip is on its way!

Check that incusdata.com is an approved sender, so that your Java tips don’t land up in the spam folder.

Our privacy policy means your data is safe. You can unsubscribe from these tips at any time.