93

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

@SuppressWarnings("deprecation")
@SuppressWarnings("unused")
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173

4 Answers4

134

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.

nfechner
  • 17,295
  • 7
  • 45
  • 64
7

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.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
6

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.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31
-4

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"
    }
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
TIANYUAN LI
  • 1
  • 1
  • 2
  • 6
    Please don't disable warning across your entire project. Disabling a warning should be scoped to the most specific level you can, where you know exactly why it is ok to ignore in your case. Hiding them for the entire project will also hide legitimate concerns you should consider changing rather than ignoring. – Michael Peterson Dec 19 '17 at 13:37