Java


JAVA.CLASS.STATICMOD : Static Field Assigned Non-Static (Java)

Summary

A static field has been modified from a non-static method.

Static fields can be updated from a static context, but this is a bad programming practice:

In object-oriented code, fields should mostly be instance fields; static fields should be used in rare situations, such as for constants. This checker looks for static fields that are assigned from non-static contexts. It only considers as acceptable assignments that are used for the lazy initialization of static fields, although they might occur in non-static contexts. That is current programming practice.

Properties

Class Name Static Field Assigned Non-Static (Java)
Significance reliability
Mnemonic JAVA.CLASS.STATICMOD
Categories
CWE CWE:1164 Irrelevant Code
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Static Field Assigned Non-Static (Java)"

Example

// Scores.java 
public class Scores {
  private static int[] scores = new int[100];
  private static int next;

  public static void main(String[] args) {
      new Scores();
  }

  public void addScore(int score) {
      if (next < scores.length)
          scores[next++] = score; /* Two "Static Field Assigned Non-Static (Java)" warning instances issued here:
                                   * - one for modification to scores
                                   * - one for modification to next
                                   */
  }

  public float average() {
      float sum = 0.0f;
      for (int pos = 0; pos < next; pos++)
          sum += scores[pos];
      return sum / next;
  }
}

In this example, and in better object-oriented style, the programmer should replace the static fields with instance fields:

  private int[] scores = new int[100];
  private int next;

Consider now the following program, which lazily initializes two static fields.

// LazyInitialisation.java 
public class LazyInitialisation {
  private static Map<String, String> session;
  private static Map<String, String> session2;

  public @EntryPoint Map<String, String> getSession() {
      if (session == null) {
          System.out.println("Initializing session");
          session = new HashMap<>();              // ok: this is a lazy initialization idiom
      }
      return session;
  }

  public @EntryPoint Map<String, String> getSession2() {
      if (session == null) {
            System.out.println("Initializing session2");
            session2 = new HashMap<>();   /* "Static Field Assigned Non-Static (Java)" warning issued here:
                                           * it occurs in a non-static context and is not part of the lazy initialization idiom.
                                           */
      }
      return session2;
  }
}       

In this example, the programmer has probably coded the lazy initialization of field session2 incorrectly and should check for nullness of session2 instead of session at the beginning of getSession2().

Resolution

Check if static fields can be replaced by instance fields or if methods can be made static.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.