What Is @Suppresswarnings Musical Note Inwards Java? Unchecked, Rawtypes, Serial
@SuppressWarnings musical note is ane of the 3 built-in annotations available inward JDK too added with @Override and @Deprecated inward Java 1.5. @SuppressWarnings teach the compiler to ignore or suppress, specified compiler alert inward annotated chemical component too all programme elements within that element. For example, if a shape is annotated to suppress a particular warning, thence a alert generated inward a method within that shape volition too last separated. You mightiness receive got seen @SuppressWarnings("unchecked") too @SuppressWarnings("serial"), 2 of most pop examples of @SuppressWarnings annotation. Former is used to suppress alert generated due to unchecked casting patch the after alert is used to remind most adding SerialVersionUID inward a Serializable class.
Since @SuppressWarnings indicates potential programming error or mistake, it's recommended to purpose them sparingly too instead endeavour to produce actual work which is triggering that warning. Having said that, at that topographic point are conditions, peculiarly patch using generics inward Java, when y'all are absolutely certain that things won't conk wrong, y'all tin purpose @SuppressWarnings to suppress those warnings.
Also, worth next are advised given past times Josh Bloch inward his famous mass Effective Java, a must read for every Java programmer, to document argue for suppressing a alert every bit a comment.
You tin acquire a listing of supported @SuppressWarnings pick for Java compiler, past times running javac -X command. Here is supported pick for javac 1.6.0_26.
You tin meet supported values of @SuppressWarnings are :
If y'all are coding inward Eclipse IDE, thence y'all are probable to meet many warnings every bit presently every bit y'all write code, which triggers whatsoever of these warnings. If y'all are compiling using javac, thence y'all won't meet all of these warnings, until y'all compile using -Xlint option.
javac -Xlint volition demo all warnings supported past times javac compiler. One of the simplest examples is implementing Serializable interface too forget to declare SerialVersionUID, which could pause your application inward time to come if the construction of shape changes.
Eclipse volition demo a alert with underlined zigzag inward yellowish color. Now if y'all desire to suppress that alert y'all tin purpose @SuppressWarnings("serial") at your shape score every bit shown below :
Similarly, if y'all receive got used raw types i.e. Collections without generic, Eclipse volition demo y'all "rawtypes" alert every bit shown below :
here, nosotros receive got used ArrayList every bit raw-type, too Eclipse is quick to dot out that. Now, if y'all type Ctrl+1 which is Eclipse shortcut for demo hint, it volition range y'all the pick to purpose @SuppressWarnings at head (method level) or at names (local variable level), ever select the lowest level, which is a local variable inward this case. If y'all select to position @SuppressWarnings("rawtypes") at the method level, Eclipse volition suppress all raw-types warnings within that method.
If nosotros compile same code without @SuprressWarnings, using javac compiler it volition demo the next hint:
too when y'all compile this code with java -Xlint, it volition demo detailed warnings:
Similarly for an unused variable, Eclipse shows warnings, which tin last suppressed past times @SuppressWarnings(unused). Remember, past times issuing these warnings, IDE too compiler aid to discovery potential bugs at code fourth dimension too compile time.
1) Never position @SuppressWarnings("unchecked) or other options, at the shape level, this would potentially mask critical warnings.
2) Use @SuppressWarnings at the smallest scope. Which way if the alert tin last removed past times putting @SuppressWarnings at a local variable thence purpose that instead of annotating whole method. It's too worth knowing that suppress warnings musical note tin last applied to class, field, method, parameter, constructor too local variables.
3) While using @SuppressWarnings("unchecked"), add together a comment to explicate most "why a particular part of code is type-safe" earlier suppressing this warning. Every unchecked alert is an indication of potential ClassCastException at runtime, thence don't ignore them.
4) Don't purpose @SuppressWarnings("all"), it volition suppress all warnings, which tin mask potential warnings which could Pb to RuntimeException when y'all run the program.
That's all on what is @SuppressWarnings inward Java, listing of options supported past times @SuppressWarnings too how to purpose them at class, method too champaign level. It's of import to purpose them correctly, too y'all tin select to pay attending to unopen to of them. At the same time, I don't propose to suppress every alert shown past times Eclipse, because it volition clutter your code with lots of annotation, instead select one's which actually matters e.g. serial, uncheck or rawtypes.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
Since @SuppressWarnings indicates potential programming error or mistake, it's recommended to purpose them sparingly too instead endeavour to produce actual work which is triggering that warning. Having said that, at that topographic point are conditions, peculiarly patch using generics inward Java, when y'all are absolutely certain that things won't conk wrong, y'all tin purpose @SuppressWarnings to suppress those warnings.
Also, worth next are advised given past times Josh Bloch inward his famous mass Effective Java, a must read for every Java programmer, to document argue for suppressing a alert every bit a comment.
List of @SuppressWarnings options on Java
Different compiler too IDEs back upwards dissimilar listing of @SuppressWarnings options. It seems IDE similar Eclipse too Netbeans back upwards to a greater extent than options than standard javac compiler.You tin acquire a listing of supported @SuppressWarnings pick for Java compiler, past times running javac -X command. Here is supported pick for javac 1.6.0_26.
$ javac -version javac 1.6.0_26 $ javac -X -Xlint Enable recommended warnings -Xlint:{all,cast,deprecation,divzero,empty,unchecked,fallthrough,path,serial,finally,overrides,-cast,-deprecation,-divzero,-empty,-unchecked,-fallthrough,-path,-serial,-finally,-overrides,none}Enable or disable specific warnings You tin meet supported values of @SuppressWarnings are :
- all
- cast
- deprecation
- divzero
- empty
- unchecked
- fallthrough
- path
- serial
- finally
- overrides
How to @SuppressWarnings inward Java?
In the finally pair of paragraphs, nosotros receive got seen what is @SuppressWarnings too listing of @SuppressWarnings options supported past times javac compiler. Now nosotros volition see, when these warnings occur too how does it aid to minimize potential errors?If y'all are coding inward Eclipse IDE, thence y'all are probable to meet many warnings every bit presently every bit y'all write code, which triggers whatsoever of these warnings. If y'all are compiling using javac, thence y'all won't meet all of these warnings, until y'all compile using -Xlint option.
javac -Xlint volition demo all warnings supported past times javac compiler. One of the simplest examples is implementing Serializable interface too forget to declare SerialVersionUID, which could pause your application inward time to come if the construction of shape changes.
Eclipse volition demo a alert with underlined zigzag inward yellowish color. Now if y'all desire to suppress that alert y'all tin purpose @SuppressWarnings("serial") at your shape score every bit shown below :
import java.io.Serializable; @SuppressWarnings("serial") public class Test implements Serializable{ private String version; public String getVersion(){ return version }; }
Similarly, if y'all receive got used raw types i.e. Collections without generic, Eclipse volition demo y'all "rawtypes" alert every bit shown below :
public class RawType{ public static void main(String args[]){ @SuppressWarnings("rawtypes") List names = new ArrayList(); } }
here, nosotros receive got used ArrayList every bit raw-type, too Eclipse is quick to dot out that. Now, if y'all type Ctrl+1 which is Eclipse shortcut for demo hint, it volition range y'all the pick to purpose @SuppressWarnings at head (method level) or at names (local variable level), ever select the lowest level, which is a local variable inward this case. If y'all select to position @SuppressWarnings("rawtypes") at the method level, Eclipse volition suppress all raw-types warnings within that method.
If nosotros compile same code without @SuprressWarnings, using javac compiler it volition demo the next hint:
$ javac RawType.java Note: RawType.java uses unchecked or dangerous operations. Note: Recompile with -Xlint:unchecked for details.
too when y'all compile this code with java -Xlint, it volition demo detailed warnings:
$ javac -Xlint RawType.java RawType.java:5: warning: [unchecked] unchecked conversion industrial plant life : java.util.ArrayList required: java.util.List List names = new ArrayList();
Similarly for an unused variable, Eclipse shows warnings, which tin last suppressed past times @SuppressWarnings(unused). Remember, past times issuing these warnings, IDE too compiler aid to discovery potential bugs at code fourth dimension too compile time.
Best Practices using @SuppressWarnings musical note inward Java
Here are pair of things, which y'all tin proceed inward heed patch using @SuppressWarnings("") annotations:1) Never position @SuppressWarnings("unchecked) or other options, at the shape level, this would potentially mask critical warnings.
2) Use @SuppressWarnings at the smallest scope. Which way if the alert tin last removed past times putting @SuppressWarnings at a local variable thence purpose that instead of annotating whole method. It's too worth knowing that suppress warnings musical note tin last applied to class, field, method, parameter, constructor too local variables.
3) While using @SuppressWarnings("unchecked"), add together a comment to explicate most "why a particular part of code is type-safe" earlier suppressing this warning. Every unchecked alert is an indication of potential ClassCastException at runtime, thence don't ignore them.
4) Don't purpose @SuppressWarnings("all"), it volition suppress all warnings, which tin mask potential warnings which could Pb to RuntimeException when y'all run the program.
That's all on what is @SuppressWarnings inward Java, listing of options supported past times @SuppressWarnings too how to purpose them at class, method too champaign level. It's of import to purpose them correctly, too y'all tin select to pay attending to unopen to of them. At the same time, I don't propose to suppress every alert shown past times Eclipse, because it volition clutter your code with lots of annotation, instead select one's which actually matters e.g. serial, uncheck or rawtypes.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


Komentar
Posting Komentar