This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Annotation good practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Annotation good practice" Watch "Annotation good practice" New topic
Author

Annotation good practice

Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
Hi,

I have a doubt...

I have a class A, which has 6 methods . two of showing unchecked warnings (generics).
I need to use




Is it good to use 'SuppressWarnings' for the two methods individually or declare 'SuppressWarnings' for the entire class....?

Regards,
Manjusha
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

It's a good idea to keep the scope of the annotation as small as possible: put it only on the methods (or even the statements inside the method) where it's needed, and not on the entire class.

In fact, it's better to not use that annotation at all - see if there is a way to rewrite your code so that you don't have any unchecked casts in it. Only if there's no way to rewrite the code, use the annotation.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Annotation good practice