Home > Java > javaTutorial > body text

@SuppressWarnings in Java

WBOY
Release: 2024-08-30 16:22:33
Original
195 people have browsed it

The @SuppressWarnings in Java is an annotation that is used to inform the compiler to suppress specified warnings for a certain part of the program. The @SuppressWarnings is a built-in annotation, as annotation like a tag representing metadata, which gives the additional information. Sometimes the warning is good, but sometimes they would be inappropriate and annoying, then the programmer can sometimes inform the compiler to suppress such a warning. Note that the specified compiler warning in The @SuppressWarnings suppress for a certain part of the program; for example, if a method is annotated to suppress a specified warning, then the compiler suppresses specified warning inside that method only, but if a class is annotated to suppress a specified warning then compiler suppresses the specified warning in a method inside that class.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The Syntax of the @SuppressWarnings annotation in java –

@SuppressWarnings( "warningOption" )
Copy after login

Parameter

  • warningOption: warningOption is a string parameter that specified a specific warning that we want the compiler to be suppressed or ignored.

The @SuppressWarnings in java support a list of different types of warnings to suppress. The Eclips and Netbean IDEs support more than standard javac compiler warning options. The list of warning option support by @SuppressWarnings is unchecked, deprecation, serial, overrides, cast, divzero, empty, fallthrough, path, finally, and all.

How @SuppressWarnings works in java with Examples?

Working and examples are mentioned below:

Example #1

Next, we write the java code to understand the @SuppressWarnings annotation more clearly with the following example where we use @SuppressWarnings to suppress the unchecked warning, as below –

Code:

//package demo;
import java.util.ArrayList;
import java.util.List;
public class Main {
@SuppressWarnings("unchecked")
// also can be write as @SuppressWarnings(value = "unchecked")
public static void main( String[] arg) {
List fruits = new ArrayList();
// this causes unchecked warning
fruits.add("Apple");
System.out.println( fruits);
}
}
Copy after login

Output:

@SuppressWarnings in Java

Explanation: As in the above code, the @SuppressWarnings annotation is annotated to the main() method as @SuppressWarnings(“unchecked”), the warning to be suppressed is unchecked warning now all code inside the main() method is also applied. So now the compiler will not issue a warning about this line “fruits.add(“Apple”)”; as it is using a raw typed collection. As in the code, it is using the @SuppressWarnings annotation to do not to fix that warning. In the above code, if we do not use the @SuppressWarnings annotation, then at line “fruits.add(“Apple”);”

Example #2

Next, we write the java code to understand the @SuppressWarnings annotation more clearly with the following example where we use @SuppressWarnings to suppress deprecationwarning, as below –

Code:

//package demo;
import javax.swing.JFrame;
public class Demo {
@SuppressWarnings("deprecation")
// also can be write as @SuppressWarnings(value = "deprecation")
public static void main( String[] arg) {
JFrame dlog = new JFrame();
dlog.setTitle("This is demo");
// this generate the deprecated warning as it is a deprecated method
// JDK version 1.7 replace it by setVisible() method
dlog.show();
dlog.setSize(500, 500);
// so use the new version method
dlog.setVisible(true);
}
}
Copy after login

Output:

@SuppressWarnings in Java

Explanation: As in the above code, the @SuppressWarnings annotation is annotated to the main() method as @SuppressWarnings(“deprecation”), the warning to be suppressed is deprecation warning. So now the compiler will not issue a warning about this line “dlog.show();” because this method is deprecated in swing API by setVisible() method. As in the code, it is using the @SuppressWarnings annotation to do not to fix that warning.

Example #3

Next, we write the java code to understand the @SuppressWarnings annotation where we use @SuppressWarnings to suppress deprecation warning at the class level, as below –

Code:

//package demo;
import javax.swing.JFrame;
@SuppressWarnings("deprecation")
public class Demo {
// also can be write as @SuppressWarnings(value = "deprecation")
public static void main( String[] arg) {
JFrame dlog = new JFrame();
dlog.setTitle("This is demo");
// this generate the deprecated warning as it is a deprecated method
// JDK version 1.7 replace it by setVisible() method
dlog.show();
dlog.setSize(500, 500);
// so use the new version method
dlog.setVisible(true);
}
public void anotherMethod()
{
JFrame dlog = new JFrame();
dlog.setTitle("This is demo");
dlog.show();
}
}
Copy after login

Output:

@SuppressWarnings in Java

Explanation: As in the above code, the @SuppressWarnings annotation is annotated to the class as @SuppressWarnings(“deprecation”), which means now all methods inside the class are also applied the warning deprecation to be to suppress, and now the compiler will not issue a warning in both methods line “dlog.show();”.

If we use the @SuppressWarnings annotation at the main() method level in the above code, the warning message will show in the anotherMethod() method, as we can clearly see in the below image.

Example #4

Next, we write the java code to understand the @SuppressWarnings annotation where we use @SuppressWarnings to suppress multiple warnings at as below –

Code:

//package demo;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
public class Demo {
@SuppressWarnings({"unchecked","deprecation"})
// also can be write as @SuppressWarnings(value={"unchecked","deprecation"})
public static void main( String[] arg) {
JFrame dlog = new JFrame();
List title = new ArrayList();       // this causes unchecked warning
title.add("This is demo");
dlog.setTitle(title.toString());
// this generate the deprecated warning as it is a deprecated method
// JDK version 1.7 replace it by setVisible() method
dlog.show();
dlog.setSize(500, 500);
// so use the new version method
dlog.setVisible(true);
}
}
Copy after login

Output:

@SuppressWarnings in Java

Explanation: As in the above code, the @SuppressWarnings annotation is annotated to the main() method as @SuppressWarnings({“unchecked”, “deprecation”}), to suppress or ignore multiple warnings “unchecked” and “deprecation”, which means the compiler will not issue a warning related to “unchecked” and “deprecation”.

Conclusion

The @SuppressWarningsis an annotation in Java which informs the compiler to ignore the specified warnings for a certain part of the program where it is annotated.

The above is the detailed content of @SuppressWarnings in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!