The @SuppressWarnings Annotation in Java
Last Updated :
24 Oct, 2021
Annotations are a very important part of Java in modern technologies, Most of the technologies such as Hibernate, Spring, Spring Boot, JPA, and so Many other Libraries are using annotations and making developers' life a lot easy. In Java, built-in General Annotations are -
- @Override
- @Deprecated
- @FunctionalInterface
- @SuppressWarnings
Syntax: The signature for Java @SuppressWarnings annotation is as follows:
@Retention(value=SOURCE)
@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })
public @interface SuppressWarnings {
String[] value;
}
As we can see, the above signature has only one element, which is Array of String, with multiple possible values.
All annotations have two properties :
- Target (@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })) - It will be used with almost everything, wherever you want to suppress warnings.
- Retention (@Retention(value=SOURCE)): Retention policy of functional Interface "SOURCE", which means annotation won't go till compiler.
Illustrations:
Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code.
1. @SuppressWarnings("unchecked")
public class Calculator {
}
- Here, it will ignore all unchecked warnings coming from that class. (All methods, variables, constructors).
2. public class Calculator {
@SuppressWarnings("unchecked")
public int sum(x,y) {
.
}
}
- It will stop warning from that function only, and not from other functions of Calculator class.
This annotation is dangerous because a warning is something potentially wrong with the code. So if we're getting any warning, the first approach should be resolving those errors. But if we're suppressing any warnings, we have to have some solid reason. The reason should be commented near to the annotation every time it is used.
Possible Values Inside @SuppressWarnings Annotation Element are as follows:
Values | Description |
---|
All | It will suppress all warnings. |
Cast | Suppress the warning while casting from a generic type to a nonqualified type or the other way around. |
Deprecation | Ignores when we're using a deprecated(no longer important) method or type. |
divzero | Suppresses division by zero warning. |
empty | Ignores warning of a statement with an empty body. |
unchecked | It doesn't check if the data type is Object or primitive. |
fallthrough | Ignores fall-through on switch statements usually (if "break" is missing). |
hiding | It suppresses warnings relative to locals that hide variable |
serial | It makes the compiler shut up about a missing serialVersionUID. |
finally | Avoids warnings relative to finally block that doesn’t return. |
unused | To suppress warnings relative to unused code. |
Note: The primary and most important benefit of using @SuppressWarnings Annotation is that if we stuck because of some known warning, then this will ignore the warning and move ahead. E.g. - deprecated and unchecked warnings.
Example:
Java
// Java Program to demonstrate Use of @SuppressWarnings
// Annotation
// Importing required packages
import java.io.*;
import java.lang.*;
import java.util.*;
// Class 1
// Helper class
class Addition {
// Method 1
public static int sum(int n1, int n2)
{
// Return the final sum
return n1 + n2;
}
// Method 2
public static int sum(int... nums)
{
int sum = 0;
for (int i : nums) {
sum += i;
}
// Return the final sum
return sum;
}
}
// Class 2
// Main class
// To test suppress warnings
public class GFG {
// Does not check if data type is Object or primitive
@SuppressWarnings("unchecked")
// Main driver method
public static void main(String[] args)
{
// Creating an object of above class in main()
// method
Addition add = new Addition();
// Ignore when we're using a deprecated
// (no longer important) method or type
@SuppressWarnings("deprecation")
int sum = Addition.sum(10, 20);
// Print and display the sum
System.out.println("Sum of 10 and 20 : " + sum);
@SuppressWarnings("rawtypes")
// Raw data type being used instead of generic
List list = new ArrayList();
// Custom input entries
list.add(12);
list.add(120);
// Print and display List elements
System.out.println("List items : " + list);
}
}
OutputSum of 10 and 20 : 30
List items : [12, 120]
Similar Reads
@SafeVarargs Annotation in Java 9 with Example
@SafeVarargs Annotation: @SafeVarargs annotation is introduced in JDK 7. @SafeVarargs annotation is used to suppress the unsafe operation warnings at the compile time. Unsafe operation warnings come at the compile time whenever in our code we invoked the method which is having varargs i.e. variable
4 min read
Suppress Warnings in MATLAB
MATLAB provides a facility to add Suppress Warnings. Using MATLAB's warning function, you can disable warning messages. Here are a few techniques: Suppress one warningThe warning ('off', 'identifier') command, where 'identifier' is the identifier of the warning message, can be used to suppress a par
2 min read
Suppress Warnings Globally in R
In this article, we are going to discuss how to suppress warnings globally in R programming language. A warning is a message that does not disturb the program flow but displays the warning along with the output. In order to suppress the warnings globally, we have to set warn=-1 in the options functi
2 min read
Java @Retention Annotations
In Java, annotations are used to attach meta-data to a program element such as a class, method, instances, etc. Some annotations are used to annotate other annotations. These types of annotations are known as meta-annotations. @Retention is also a meta-annotation that comes with some retention polic
3 min read
Java - @Target Annotations
Annotations in java are used to associate metadata to program elements like classes, methods, instance variables, etc. There are mainly three types of annotations in java: Marker Annotation (without any methods), Single-Valued Annotation (with a single method), and Multi-Valued Annotation (with more
3 min read
Annotations in Java
Annotations are used to provide supplemental information about a program. Annotations start with â@â.Annotations do not change the action of a compiled program.Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.A
9 min read
Spring AOP - AspectJ Annotation
AOP i.e Aspect-Oriented Programming complements OOP by enabling modularity of cross-cutting concerns. @AspectJ is mainly used for declaring aspects as regular Java classes annotated with annotations. Spring AspectJ AOP implementation has many annotations. They are explained below: @Aspect: It declar
4 min read
The @Deprecated Annotation in Java
The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is. It's no longer relevant. It is so unimportant that you should stop using it because it has been su
5 min read
Spring Conditional Annotations
The Spring Framework provides a powerful way to control bean creation and configuration based on specific conditions through its conditional annotations. These features can be particularly useful in building flexible and environment-specific configurations without cluttering the application with boi
5 min read
Spring @Component Annotation with Example
Spring is one of the most popular frameworks for building enterprise applications in Java. It is an open-source, lightweight framework that allows developers to build simple, reliable, and scalable applications. Spring focuses on providing various ways to manage business objects efficiently. It simp
3 min read