Skip to content
Related Articles
Open in App
Not now

Related Articles

The @SuppressWarnings Annotation in Java

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 24 Oct, 2021
Improve Article
Save Article

Annotations are a very important part of Java in modern technologies, Most of the technologies such as Hibernate, Spring, Spring Boot, JPA, and so Many other Libraries are using annotations and making developers’ life a lot easy. In Java, built-in General Annotations are – 

  1. @Override
  2. @Deprecated
  3. @FunctionalInterface
  4. @SuppressWarnings

Syntax: The signature for Java @SuppressWarnings annotation is as follows:

public @interface SuppressWarnings {
       String[] value;

As we can see, the above signature has only one element, which is Array of String, with multiple possible values.

All annotations have two properties :

  1. Target (@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE }))  – It will be used with almost everything, wherever you want to suppress warnings.
  2. Retention (@Retention(value=SOURCE)): Retention policy of functional Interface “SOURCE”, which means annotation won’t go till compiler.


Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code.

1. @SuppressWarnings("unchecked")
   public class Calculator {
- Here, it will ignore all unchecked warnings coming from that class. (All methods, variables, constructors).
2. public class Calculator {
      public int sum(x,y) {
- It will stop warning from that function only, and not from other functions of Calculator class.

This annotation is dangerous because a warning is something potentially wrong with the code. So if we’re getting any warning, the first approach should be resolving those errors. But if we’re suppressing any warnings, we have to have some solid reason. The reason should be commented near to the annotation every time it is used.

Possible Values Inside @SuppressWarnings Annotation Element  are as follows:


All It will suppress all warnings.
Cast         Suppress the warning while casting from a generic type to a nonqualified type or the other way around.
  Deprecation   Ignores when we’re using a deprecated(no longer important) method or type.
divzeroSuppresses division by zero warning.
emptyIgnores warning of a statement with an empty body.
unchecked It doesn’t check if the data type is Object or primitive.
fallthrough Ignores fall-through on switch statements usually (if “break” is missing).
hiding It suppresses warnings relative to locals that hide variable
serialIt makes the compiler shut up about a missing serialVersionUID.
finallyAvoids warnings relative to finally block that doesn’t return.
unusedTo suppress warnings relative to unused code.

Note: The primary and most important benefit of using @SuppressWarnings Annotation is that if we stuck because of some known warning, then this will ignore the warning and move ahead. E.g. – deprecated and unchecked warnings.



// Java Program to demonstrate Use of @SuppressWarnings
// Annotation
// Importing required packages
import java.lang.*;
import java.util.*;
// Class 1
// Helper class
class Addition {
    // Method 1
    public static int sum(int n1, int n2)
        // Return the final sum
        return n1 + n2;
    // Method 2
    public static int sum(int... nums)
        int sum = 0;
        for (int i : nums) {
            sum += i;
        // Return the final sum
        return sum;
// Class 2
// Main class
// To test suppress warnings
public class GFG {
    // Does not check if data type is Object or primitive
    // Main driver method
    public static void main(String[] args)
        // Creating an object of above class in main()
        // method
        Addition add = new Addition();
        // Ignore when we're using a deprecated
        // (no longer important) method or type
        int sum = Addition.sum(10, 20);
        // Print and display the sum
        System.out.println("Sum of 10 and 20 : " + sum);
        // Raw data type being used instead of generic
        List list = new ArrayList();
        // Custom input entries
        // Print and display List elements
        System.out.println("List items : " + list);



Sum of 10 and 20 : 30
List items : [12, 120]


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!