7

I had used @SuppressWarnings("unchecked") like:

class SomeClass {
  public static void main(String [] args) {
    Vector v = new Vector();
    @SuppressWarnings("unchecked")
    v.addElement(new Integer(1_000_000));
    // ...        

On compilation using javac version 8.0, I got the following errors:

error: <identifier> expected
        v.addElement(new Integer(1_000_000));

with the caret (^) pointing at the opening bracket of addElement method and one more error message complaining missing semicolon(;). The second message showed the caret near the closing bracket of addElement.

However, when I moved the @SuppressWarnings("unchecked") above the class SomeClass{, line, as in:

@SuppressWarnings("unchecked")
class SomeClass {

both the error messages disappeared automagically. This baffled me to any end. Is the positioning of @SuppressWarnings("unchecked") so critical?

Seshadri R
  • 1,192
  • 14
  • 24
  • `@SuppressWarnings` is used to well, literally suppress any warning thrown. – Kyle Emmanuel Sep 07 '16 at 09:36
  • Yes, the @SuppressWarnings annotatioj is important. Without it, the compiler will issue a warning, about unsafe operations.You can check this : http://www.codejava.net/java-core/the-java-language/suppresswarnings-annotation-examples –  Sep 07 '16 at 09:37

3 Answers3

9

You must not place annotations like @SuppressWarnings("unchecked") on a statement but only on a declaration.

That's why the compiler does not understand the first variant:

@SuppressWarnings("unchecked")
v.addElement(new Integer(1_000_000));

It's just illegal there and the compiler won't understand it.

You could put it on the method instead, if you want to reduce the scope:

@SuppressWarnings("unchecked")
public static void main(String [] args) {
    Vector v = new Vector();
    v.addElement(new Integer(1_000_000));

Thus, only warnings for the main method will be ignored, but not for the whole class.

You can even put it on the variable declaration, but never on a statement:

@SuppressWarnings("unchecked")
Vector v = new Vector();

However, as GhostCat has pointed out, in this case it is probably better to just generify the vector instead of ignoring warnings.

Michaela Elschner
  • 1,268
  • 1
  • 10
  • 22
  • Thanks for the tip "probably better to just generify the vector instead of ignoring warnings". I wanted to use the vector v to collect elements of different types. Hence, I don't have the option to generify it. – Seshadri R Sep 12 '16 at 06:32
  • @SeshadriR You could use a Vector. However, an ArrayList would be even more preferrable. – Michaela Elschner Sep 12 '16 at 07:36
3

One could say: you can't place this annotation on statements, such as v.add(1); instead it should go on a declaration, but that answer would leave out to many important aspects here.

Thing is: you can use annotations to prevent the compiler giving you warnings on the one hand. On the other hand, you only want to suppress those warnings that you understand, and that you consider "ok to be suppressed".

Coming from that point of view, you always to annotate on the "smallest" level possible. Thus, the annotation would go here:

@SuppressWarnings("unchecked")
Vector v = new Vector();

But of course, in your case the warning is absolutely valid; and suppressing it is wrong.

Just change your code to:

Vector<Integer> v = new Vector<>();

and voilà no more need for the annotation at all.

In other words: you work your way from "in to out". Meaning: if you need to suppress warnings, you first go for the individual variables that give you the warning. And only if you got too many such statements in one method, you put the annotation on the method. You never ever want to annotate the whole class. Because that would suppress all such warnings in the whole class.

And honestly: the real lesson learned here: study the concepts you are using. The compiler is giving you a warning because you are using a raw type (by omitting the type parameter when declaring v). Then ignoring that warning is wrong wrong wrong. You figure what the warning is telling you, and then you fix your code instead!

"Good code" is completely warning free; and it has as few "suppress" annotations as possible!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Yes the placement of this annotation as is for any other annotation matters, this is defined by the annotation @Target used in the defnition of the this annotation itself.

For @SupressWarnings it is

@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})

But you placed it on a statement which ist non of the above. You should at least place it on the local variable declaration level:

@SuppressWarnings("unchecked")
Vector v = new Vector();
A4L
  • 17,353
  • 6
  • 49
  • 70