Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2386

Mutable fields should not be "public static"

    Details

    • Message:
      Hide
      * Move "xxx" to a class and lower its visibility.
      * Make this member "protected".
      Show
      * Move "xxx" to a class and lower its visibility. * Make this member "protected".
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      C#, Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources
    • Common Rule:
      Yes
    • CERT:
      OBJ01-J., OBJ13-J.
    • CWE:
      CWE-607, CWE-582
    • FindBugs:
      MS_OOI_PKGPROTECT, MS_MUTABLE_ARRAY, MS_MUTABLE_HASHTABLE, MS_MUTABLE_COLLECTION, MS_MUTABLE_COLLECTION_PKGPROTECT, MS_FINAL_PKGPROTECT, MS_PKGPROTECT

      Description

      There is no good reason to have a mutable object as the public (by default), static member of an interface. Such variables should be moved into classes and their visibility lowered.

      Similarly, mutable static members of classes and enumerations which are accessed directly, rather than through getters and setters, should be protected to the degree possible. That can be done by reducing visibility or making the field final if appropriate.

      Note that making a mutable field, such as an array, final will keep the variable from being reassigned, but doing so has no effect on the mutability of the internal state of the array (i.e. it doesn't accomplish the goal).

      This rule raises issues for public static array, Collection, Date, and awt.Point members.

      Noncompliant Code Example

      public interface MyInterface {
        public static String [] strings; // Noncompliant
      }
      
      public class A {
        public static String [] strings1 = {"first","second"};  // Noncompliant
        public static String [] strings2 = {"first","second"};  // Noncompliant
        public static List<String> strings3 = new ArrayList<>();  // Noncompliant
        // ...
      }
      

      See

        Attachments

          Issue Links

          1.
          C# RSPEC-3888 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ann.campbell.2 Ann Campbell
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: