Javatpoint Logo
Javatpoint Logo

@SuppressWarnings Annotation in Java

In Java, the @SuppressWarnings is defined as an annotation that is utilized for suppressing or ignoring particular warnings which are raised by the compiler because of a specific code. In simple words, the compiler gets indicated by the @SuppressWarnings annotation to pass over or ignore particular warnings that are generated due to specific code. This gives a technique for overriding the pre-defined warnings that might be generated.

The java.lang package contains the @SuppressWarnings annotation in Java. Let's understand about the @SuppressWarnings annotation in Java with the help of an example.

Syntax:

In the above-mentioned example, the compiler will be instructed to ignore or suppress all the warnings and errors that will be generated by the SuppressAnnotationExample class including all the methods, static and non-static variables and constructors.

How to Use The @SuppressWarnings Annotation in Java?

In Java, the @SuppressWarnings annotation can be claimed regarding various elements of Java such as methods, classes, static and non-static variables etc. This annotation accepts a string variable or value as it's argument. This string value represents a type of warning that needs to be suppressed in the mentioned area of code.

In the above-mentioned example, the Create() method is creating an ArrayList without mentioning or utilizing the generics and a string value is added to the ArrayList. This would generate an error as no wrapper class is mentioned for the ArrayList while declaration. Nonetheless, the usage of @SuppressWarnings("unchecked") annotation will be utilized for suppressing the error or warning that will be generated.

Values Applicable Inside @SuppressWarnings Annotation in Java

There are various types of values that are applicable to be used inside @SuppressWarnings annotation in Java. Each value plays an important role. Let us understand in detail with the help of below-mentioned tabular data.

Values Description
All This value is utilized for suppressing all types and kinds of warnings that are generated by the compiler.
Cast This value is utilized for suppressing the warning that is generated when a casting is performed from a generic type into a non-qualified type or vice versa.
Deprecation This value is utilized for suppressing the warning when a deprecated method or type is being used.
divzero This value is utilized for suppressing the warning generated while division by zero is performed.
empty This value is utilized for suppressing the warning generated due to a statement consisting an empty body.
unchecked This value is utilized for suppressing the warning generated when the data type is not checked whether it is an Object or primitive.
fallthrough Used for ignoring warnings generated within a switch statement when the break statement is missing.
hiding This value is utilized for suppressing the warnings that are relative to locals whose variables are hidden.
serial This value is utilized for suppressing the warning that is generated when a serialVersionUID is missing.
finally This value is utilized for suppressing the warning that are generated by finally block which does not return.
unused This value is utilized for suppressing warnings that are related to code which is unused.

These were all the types of values that were accepted by the @SuppressWarnings annotation in Java. Let us understand more about the @SuppressWarnings annotation in Java with the help of a Java example program.

Example 1:

Filename: SuppressAnnotationExample.java

Output:

The Result obtained by Addition of 100 and 200: 300
Values stored in the List : [1, 2]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA