How to suppress unchecked warnings – Java

The ‘unchecked warnings’ is quite popular warning message in Java. However, if you insist this is an invalid warning, and there are no ways to solve it without compromising the existing program functionality. You may just use @SuppressWarnings(“unchecked”) to suppress unchecked warnings in Java.

1. In Class

If applied to class level, all the methods and members in this class will ignore the unchecked warnings message.

@SuppressWarnings("unchecked")
public class classA{...}

2. In Method

If applied to method level, only this method will ignore the unchecked warnings message.

@SuppressWarnings("unchecked")
private void method1(){...}

3. In Property

If applied to property level, only this property will ignore the unchecked warnings message.

@SuppressWarnings("unchecked")
private List list1;

As conclusion, suppress an unchecked warning is like hiding a potential bug, it’s better to find the cause of the unchecked warning and fix it :)

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • http://ampkumar.blogspot.com kumar

    Greetings!
    Thanks for the solution
    2 solution suited for applctn
    when i tried to use @SuppressWarnings(“unchecked”) it still says previous uncheked warning why!

    give me more briefing on sol1 and sol2
    if sol1 used any problem to the desired result?
    thanks in advance!

    • http://www.mkyong.com mkyong

      Sol1 apply to the class level, pls provide your code.

  • maninder

    sir i am working on jdk ver 6.0 I am getting a type unchecked warning when i am compiling a code what to do to remove a error

    code

    package com.example.model;
    import java.util.*;
    import java.lang.*; 
     
    public class BeerExpert 
    {
    public List getBrands(String color)  {
    List brands = new ArrayList();
    if (color.equals("amber")){
    brands.add("Jack Amber ");
    brands.add("Red Moose");
     
    }
    else{
    brands.add("Jail Pale Ale");
    brands.add("Gout Stout");
    }
    return(brands);
    }
    }
    • http://www.mkyong.com mkyong

      It caused by the generic data type is undefined in your List.

      Solution ..

      1. Add @SuppressWarnings(“unchecked”) to ignore the checking.

      @SuppressWarnings("unchecked")
      public class test
      {

      2. Declare the data type for your List

      package com.mkyong.common;
       
      import java.util.*;
       
      public class test
      {
      	public static void main( String[] args )
      	{
      		 List<String> colors = getBrands("amber");
      		 for(String color: colors){
      			 System.out.println("Color : " + color);
      		 }
      	}
       
      	public static List<String> getBrands(String color) {
      		List<String> brands = new ArrayList<String>();
      		if (color.equals("amber")){
      			brands.add("Jack Amber");
      			brands.add("Red Moose");
      		}else{
      			brands.add("Jail Pale Ale");
      			brands.add("Gout Stout");
      		}
      		return(brands);
      	}
      }