What do @SuppressWarnings("deprecation") and ("unused") mean in Java?

JavaAndroidSuppress Warnings

Java Problem Overview


What do these lines in my Java or Android project mean?

@SuppressWarnings("deprecation")
@SuppressWarnings("unused")

Java Solutions


Solution 1 - Java

The @SuppressWarnings annotation disables certain compiler warnings. In this case, the warning about deprecated code ("deprecation") and unused local variables or unused private methods ("unused"). This article explains the possible values.

Solution 2 - Java

One more thing: you can not only add them inline, but also annotate methods. For example

@Override
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

Yet, it is recommended to use the smallest scope possible

> As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.

Solution 3 - Java

In Java, @SuppressWarnings are use to restrict the compiler to show the certain warning on the console screen.

E.g

@SuppressWarnings("unused")
CheckBox transferredField = new CheckBox("is transferred");
	

if I don't use transferredField variable in my code then your Eclipse IDE never show the warning that you are not using this transferredField variable in your code.

Solution 4 - Java

the @SuppressWarnings("unused") and others i have tried,but it don't work,and i always use the method say gradle in this project to

lintOptions{
        checkReleaseBuilds false
        abortOnError  false;
        disable 'deprecation'
    }

    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPraveenkumarView Question on Stackoverflow
Solution 1 - JavanfechnerView Answer on Stackoverflow
Solution 2 - Javaserv-incView Answer on Stackoverflow
Solution 3 - JavaYasir Shabbir ChoudharyView Answer on Stackoverflow
Solution 4 - JavaTIANYUAN LIView Answer on Stackoverflow