Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

Code Block
bgColor#FFCCCC
public static final HashMap<IntegerHashMap&lt;Integer, String>String&gt; hm = new HashMap<IntegerHashMap&lt;Integer, String>String&gt;();

Compliant Solution

Mutable data members that are static must always be declared private.

Code Block
bgColor#ccccff
private static final HashMap<IntegerHashMap&lt;Integer, String>String&gt; hm = new HashMap<IntegerHashMap&lt;Integer, String>String&gt;();

Exceptions

Wiki Markup
*EX1:* According to Sun's Code Conventions document \[[Conventions 09|AA. Java References#Conventions 09]\]:

One example of appropriate public instance variables is the case where the class is essentially a data structure, with no behavior. In other words, if you would have used a struct instead of a class (if Java supported struct), then it's appropriate to make the class's instance variables public.

Wiki Markup
*EX2:* "&quot;if a class is package-private or is a {{private}} nested class, there is nothing inherently wrong with exposing its data fields - assuming they do an adequate job of describing the abstraction provided by the class. This approach generates less visual clutter than the accessor-method approach, both in the class definition and in the client code that uses it."&quot; \[[Bloch 08|AA. Java References#Bloch 08]\]. This exception applies to both mutable as well as immutable fields.

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C++ Secure Coding Standard as OBJ00-CPP. Declare data members private.

...

08. Object Orientation (OBJ)      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;08. Object Orientation (OBJ)      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OBJ01-J. Understand how a superclass can affect a subclass