3

Is it possible to suppress this kind of warning?

Note: I am not looking for suppressing all warnings(like this @SuppressWarnings("all") ), but only the mentioned type.

Flowryn
  • 1,437
  • 1
  • 16
  • 32
  • 4
    I suggest you make the field final. Using non-final fields is error prone. Note: this is not a Java warning so it depends on which tool you are using. – Peter Lawrey Jan 27 '16 at 12:17
  • @PeterLawrey I am using Android Studio. I am reading a book related to android gaming and they use a non-final field which they update from time to time. Also the field is synchronized in both class where is accessed therefore it seems to be safe. – Flowryn Jan 28 '16 at 07:42
  • Update: the field is set only once, theoretically it could be injected via constructor and set as final, however I see they have a setter method as a convention in a interface that I inherit. – Flowryn Jan 28 '16 at 07:49

3 Answers3

5

For Intellij, put this annotation on the class that has the warnings

@SuppressWarnings("SynchronizeOnNonFinalField")

This lead me to the tag to use for suppression and trial and error lead me to put it on the class instead of on the field or synchronized statement. :-P

stuckj
  • 977
  • 1
  • 13
  • 24
  • 1
    NOTE: I'm doing this in Android Studio...I'm assuming it also works for Intellij for normal Java projects. – stuckj Mar 08 '16 at 20:30
  • It works on both - tested it. Thank you as well for the link which helped you find the annotation tag – Flowryn Mar 09 '16 at 03:54
3

If you're using IntelliJ, it looks like it's @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter"). Otherwise it depends. You might find the answer on the list here: What is the list of valid @SuppressWarnings warning names in Java?

Note that the warning is usually correct. In most cases you should just use a final field.

Community
  • 1
  • 1
  • I am using Android Studio but it should be the same. I tested on both and does not work the suppress you mention above. – Flowryn Jan 28 '16 at 07:56
0

This would help you,

All values are permitted (unrecognized ones are ignored). The list of recognized ones is compiler specific.

'unchecked' and 'deprecation' are required by the Java Language Specification, and so should be valid with all compilers. For Sun's compiler, running 'javac -X' gives a list of all values recognized by that version. For 1.5.0_17, the list appears to be:

all deprecation unchecked fallthrough path serial finally

Please refer this, same has discussed here What is the list of valid @SuppressWarnings warning names in Java?

Community
  • 1
  • 1
Santosh
  • 362
  • 4
  • 15