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 :)

About the Author

author image
mkyong
Founder of Mkyong.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

Leave a Reply

avatar
newest oldest most voted
kumar
Guest
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!

maninder
Guest
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);
}
}