@SuppressWarnings notation is ane of the iii built-in annotations available inward JDK as well as added amongst @Override and @Deprecated inward Java 1.5. @SuppressWarnings learn the compiler to ignore or suppress, specified compiler alert inward annotated chemical component as well as all programme elements within that element. For example, if a degree is annotated to suppress a particular warning, therefore a alert generated inward a method within that degree volition equally good live separated. You mightiness direct keep seen @SuppressWarnings("unchecked") as well as @SuppressWarnings("serial"), 2 of most pop examples of @SuppressWarnings annotation. Former is used to suppress alert generated due to unchecked casting piece the afterward alert is used to remind nearly adding SerialVersionUID inward a Serializable class.
Since @SuppressWarnings indicates potential programming error or mistake, it's recommended to role them sparingly as well as instead endeavour to produce actual employment which is triggering that warning. Having said that, at that topographic point are conditions, peculiarly piece using generics inward Java, when yous are absolutely certain that things won't pop off wrong, yous tin role @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 equally a comment.
You tin acquire a listing of supported @SuppressWarnings selection for Java compiler, past times running javac -X command. Here is supported selection for javac 1.6.0_26.
You tin come across supported values of @SuppressWarnings are :
If yous are coding inward Eclipse IDE, therefore yous are probable to come across many warnings equally shortly equally yous write code, which triggers whatever of these warnings. If yous are compiling using javac, therefore yous won't come across all of these warnings, until yous compile using -Xlint option.
javac -Xlint volition demo all warnings supported past times javac compiler. One of the simplest examples is implementing Serializable interface as well as forget to declare SerialVersionUID, which could intermission your application inward futurity if the construction of degree changes.
Eclipse volition demo a alert with underlined zigzag inward yellowish color. Now if yous desire to suppress that alert yous tin role @SuppressWarnings("serial") at your degree bird equally shown below :
Similarly, if yous direct keep used raw types i.e. Collections without generic, Eclipse volition demo yous "rawtypes" alert equally shown below :
here, nosotros direct keep used ArrayList equally raw-type, as well as Eclipse is quick to dot out that. Now, if yous type Ctrl+1 which is Eclipse shortcut for demo hint, it volition give yous the selection to role @SuppressWarnings at top dog (method level) or at names (local variable level), ever select the lowest level, which is a local variable inward this case. If yous select to set @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:
as well as when yous compile this code with java -Xlint, it volition demo detailed warnings:
Similarly for an unused variable, Eclipse shows warnings, which tin live suppressed past times @SuppressWarnings(unused). Remember, past times issuing these warnings, IDE as well as compiler aid to uncovering potential bugs at code fourth dimension as well as compile time.
1) Never set @SuppressWarnings("unchecked) or other options, at the degree level, this would potentially mask critical warnings.
2) Use @SuppressWarnings at the smallest scope. Which agency if the alert tin live removed past times putting @SuppressWarnings at a local variable therefore role that instead of annotating whole method. It's equally good worth knowing that suppress warnings notation tin live applied to class, field, method, parameter, constructor as well as local variables.
3) While using @SuppressWarnings("unchecked"), add together a comment to explicate nearly "why a particular part of code is type-safe" earlier suppressing this warning. Every unchecked alert is an indication of potential ClassCastException at runtime, therefore don't ignore them.
4) Don't role @SuppressWarnings("all"), it volition suppress all warnings, which tin mask potential warnings which could Pb to RuntimeException when yous run the program.
That's all on what is @SuppressWarnings inward Java, listing of options supported past times @SuppressWarnings as well as how to role them at class, method as well as champaign level. It's of import to role them correctly, as well as yous tin select to pay attending to only about of them. At the same time, I don't advise 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 role them sparingly as well as instead endeavour to produce actual employment which is triggering that warning. Having said that, at that topographic point are conditions, peculiarly piece using generics inward Java, when yous are absolutely certain that things won't pop off wrong, yous tin role @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 equally a comment.
List of @SuppressWarnings options on Java
Different compiler as well as IDEs back upwards dissimilar listing of @SuppressWarnings options. It seems IDE similar Eclipse as well as Netbeans back upwards to a greater extent than options than standard javac compiler.You tin acquire a listing of supported @SuppressWarnings selection for Java compiler, past times running javac -X command. Here is supported selection 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 come across supported values of @SuppressWarnings are :
- all
- cast
- deprecation
- divzero
- empty
- unchecked
- fallthrough
- path
- serial
- finally
- overrides
How to @SuppressWarnings inward Java?
In the lastly duad of paragraphs, nosotros direct keep seen what is @SuppressWarnings as well as listing of @SuppressWarnings options supported past times javac compiler. Now nosotros volition see, when these warnings range as well as how does it aid to minimize potential errors?If yous are coding inward Eclipse IDE, therefore yous are probable to come across many warnings equally shortly equally yous write code, which triggers whatever of these warnings. If yous are compiling using javac, therefore yous won't come across all of these warnings, until yous compile using -Xlint option.
javac -Xlint volition demo all warnings supported past times javac compiler. One of the simplest examples is implementing Serializable interface as well as forget to declare SerialVersionUID, which could intermission your application inward futurity if the construction of degree changes.
Eclipse volition demo a alert with underlined zigzag inward yellowish color. Now if yous desire to suppress that alert yous tin role @SuppressWarnings("serial") at your degree bird equally shown below :
import java.io.Serializable; @SuppressWarnings("serial") public class Test implements Serializable{ private String version; public String getVersion(){ return version }; }
Similarly, if yous direct keep used raw types i.e. Collections without generic, Eclipse volition demo yous "rawtypes" alert equally shown below :
public class RawType{ public static void main(String args[]){ @SuppressWarnings("rawtypes") List names = new ArrayList(); } }
here, nosotros direct keep used ArrayList equally raw-type, as well as Eclipse is quick to dot out that. Now, if yous type Ctrl+1 which is Eclipse shortcut for demo hint, it volition give yous the selection to role @SuppressWarnings at top dog (method level) or at names (local variable level), ever select the lowest level, which is a local variable inward this case. If yous select to set @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.
as well as when yous compile this code with java -Xlint, it volition demo detailed warnings:
$ javac -Xlint RawType.java RawType.java:5: warning: [unchecked] unchecked conversion establish : java.util.ArrayList required: java.util.List List names = new ArrayList();
Similarly for an unused variable, Eclipse shows warnings, which tin live suppressed past times @SuppressWarnings(unused). Remember, past times issuing these warnings, IDE as well as compiler aid to uncovering potential bugs at code fourth dimension as well as compile time.
Best Practices using @SuppressWarnings notation inward Java
Here are duad of things, which yous tin pop off along inward hear piece using @SuppressWarnings("") annotations:1) Never set @SuppressWarnings("unchecked) or other options, at the degree level, this would potentially mask critical warnings.
2) Use @SuppressWarnings at the smallest scope. Which agency if the alert tin live removed past times putting @SuppressWarnings at a local variable therefore role that instead of annotating whole method. It's equally good worth knowing that suppress warnings notation tin live applied to class, field, method, parameter, constructor as well as local variables.
3) While using @SuppressWarnings("unchecked"), add together a comment to explicate nearly "why a particular part of code is type-safe" earlier suppressing this warning. Every unchecked alert is an indication of potential ClassCastException at runtime, therefore don't ignore them.
4) Don't role @SuppressWarnings("all"), it volition suppress all warnings, which tin mask potential warnings which could Pb to RuntimeException when yous run the program.
That's all on what is @SuppressWarnings inward Java, listing of options supported past times @SuppressWarnings as well as how to role them at class, method as well as champaign level. It's of import to role them correctly, as well as yous tin select to pay attending to only about of them. At the same time, I don't advise 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!