JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


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

要旨

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.

プロパティ

クラス名 Static Field Assigned Non-Static (Java)
日本語クラス名 Static Field Assigned Non-Static (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.CLASS.STATICMOD
カテゴリー
CWE CWE:1164 Irrelevant Code
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Static Field Assigned Non-Static (Java)"

// 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().

解決法

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

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.