5

I didn't understand this feature of Java. I know it makes coding easier and sometimes looks neater, but what is the actual use of this? On contrary i feel, its better to display the warnings, as in future any one can refer them before making modifications to code. Does this @SuppressWarnings increases compliling efficiency OR is this according to any coding standards?

freepublicview
  • 646
  • 2
  • 12
  • 24

6 Answers6

9

While programming you should watch out for compiler warnings and in best case compile your code with no warnings (and errors of course).

But sometimes you can't get rid of a warning and you KNOW the code is correct or can't be changed. Then you don't want to be bothered every time from the compiler that there is something wrong.

So you can suppress it with the command you mentioned.

juergen d
  • 186,950
  • 30
  • 261
  • 325
7

Other answers already explained use cases of @SuppressWarnings a lot, but I want to emphasize the point that sometimes you absolutely need to use @SuppressWarnings to overcome limitations of the language itself, and in these cases use of @SuppressWarnings is absolutely legal.

In other cases use of @SuppressWarnings can be considered questionable, because in these cases you can always get rid of warnings by changing the code (though, obviously, it's not always acceptable).

Here are some common cases when you absolutely cannot get rid of warnings without @SuppressWarnings:

  • Implementing an array-backed generic collection (since you cannot create a generic array)
  • A Map from classes to their implementations (since you cannot assign different values of generic type parameter to different map entires)
axtavt
  • 228,184
  • 37
  • 489
  • 472
4

When you are dealing with legacy code that does not support generics (Java <= 1.4) that is the only viable way of geting rid of cast warnings.

Wojtek Owczarczyk
  • 5,206
  • 2
  • 27
  • 53
  • 2
    `SuppressWarnings` are not only used for Generics support or cast warnings. – Buhake Sindi Dec 20 '11 at 09:31
  • 2
    I only said it is the only way of removing cast warnings due to lack of generics in older parts of the code, when at some point you would like to introduce them. I haven't said it is the only use of this annotation... – Wojtek Owczarczyk Dec 20 '11 at 09:50
1

The actual use is to suppress warnings. That's it. Nothing less, nothing more. I don't think it affects any compiling efficiency but definitely not run time efficiency.

Ranhiru Jude Cooray
  • 18,386
  • 17
  • 80
  • 123
1

The use of SuppressWarnings is to be used during compilation (so, only the compiler knows what to do when it sees the SuppressWarnings annotation).

The JavaDoc states:

The set of warnings that are to be suppressed by the compiler in the annotated element. Duplicate names are permitted. The second and successive occurrences of a name are ignored. The presence of unrecognized warning names is not an error: Compilers must ignore any warning names they do not recognize. They are, however, free to emit a warning if an annotation contains an unrecognized warning name.

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
1

There are several reasons for using @SuppressWarnings but one of the most useful cases are:

  • Having an existing project with thousands of warnings. You want your team to take care about warnings now but you do not have the time to fix all warnings.

    The Annotation gives you the opportunity to suppress some warnings that cannot be fixed fast and get a "clean" base.

    This clean base is essential since nobody will be afraid of checking in code with warnings when there are thousands of it, but they will be afraid when the codebase is warning-free and everybody else in the team immediatly see when someone is checking in code with 2 warnings or so.

  • You are using 3rd party libs and you are therefore getting annoyed with warnings you cannot fix except for changing lib's code what you do not want in most cases.

In my opinion the first reason is one of the best use cases for @SuppressWarnings.

Fabian Barney
  • 13,133
  • 4
  • 37
  • 60