Excluding Warnings Using @SuppressWarnings

Since Java 5.0, you can disable compilation warnings relative to a subset of a compilation unit using the java.lang.SuppressWarning annotation.

 @SuppressWarning("unused") public void foo() {
  String s;
 }

Without the annotation, the compiler would complain that the local variable s is never used. With the annotation, the compiler silently ignores this warning locally to the foo method. This enables to keep the warnings in other locations of the same compilation unit or the same project.

The list of tokens that can be used inside a SuppressWarnings annotation is:

Token

Description

Suppressed compiler option(s)

all

to suppress all warnings

All compiler options

boxing

to suppress warnings relative to boxing/unboxing operations

Boxing and unboxing conversions

cast

to suppress warnings relative to cast operations

Unnecessary cast or 'instanceof' operation

dep-ann

to suppress warnings relative to deprecated annotation

Missing '@Deprecated' annotation

deprecation

to suppress warnings relative to deprecation

Deprecated API

fallthrough

to suppress warnings relative to missing breaks in switch statements

'switch' case fall-through

finally

to suppress warnings relative to finally block that don't return

'finally' does not complete normally

hiding

to suppress warnings relative to locals that hide variable

Local variable declaration hides another field or variable

incomplete-switch

to suppress warnings relative to missing entries in a switch statement (enum case)

Incomplete 'switch' cases on enum

javadoc

to suppress warnings relative to javadoc warnings

All options in Java Compiler Javadoc Preferences

nls

to suppress warnings relative to non-nls string literals

Non-externalized strings

null

to suppress warnings relative to null analysis

All options in Java Compile Errors/Warnings Preferences for Null Analysis

rawtypes

to suppress warnings relative to usage of raw types

Usage of a raw type

resource

to suppress warnings relative to usage of resources of type Closeable

Resource leak
Potential resource leak
Resource not managed via try-with-resource

restriction

to suppress warnings relative to usage of discouraged or forbidden references

Forbidden reference (access rules)
Discouraged reference (access rules)

removal

to suppress warnings relative to usage of terminally deprecated API

Forbidden reference (access rules)
Discouraged reference (access rules)

serial

to suppress warnings relative to missing serialVersionUID field for a serializable class

Serializable class without serialVersionUID

static-access

to suppress warnings relative to incorrect static access

Non-static access to a static member
Indirect access to a static member

static-method

to suppress warnings relative to methods that could be declared as static

Method can be static
Method can potentially be static

super

to suppress warnings relative to overriding a method without super invocations

synthetic-access

to suppress warnings relative to unoptimized access from inner classes

sync-override

to suppress warnings because of missing synchronize when overriding a synchronized method

Missing synchronized modifier on inherited method

unchecked

to suppress warnings relative to unchecked operations

Unchecked generic type operation

unqualified-field-access

to suppress warnings relative to field access unqualified

Unqualified access to instance field

unlikely-arg-type

to suppress warnings relative to unlikely argument types

Unqualified access to instance field

unused

to suppress warnings relative to unused code and dead code

Dead code
Unused object allocation
Value of local variable is not used
Value of method parameter is not used
Value of exception parameter is not used
Unused type parameter
Unused import
Unused private member
Unused 'break' or 'continue' label

Java Compile Errors/Warnings Preferences