[Java] S2696 raises no issue if method is synchronized but not static


(Oliver Goroll) #1

The description of rule S2696 states that static fields should only be set in synchronized static methods. The rule however is already not violated if the method is synchronized but not static.

The following Code results in two errors, one stating that the lazy initialization should be synchronized (S2444) and that instance methods should not update static fields:

class A {
  private static String field;
  public void setField(String field) {
    this.field = field;
   }
 }

The following code fixes boths violations although setField(String) is still an instance method:

class A {
  private static String field;
  public synchronized void setField(String field) {
    this.field = field;
   }
 }