...
| Code Block | ||
|---|---|---|
| ||
public class Widget {
public int total; // Number of elements
void add() {
if (total < Integer.MAX_VALUE) {
total++;
// ...
} else {
throw new ArithmeticException("Overflow");
}
}
void remove() {
if (total > 0) {
total--;
// ...
} else {
throw new ArithmeticException("Overflow");
}
}
}
|
As a public data member, {{Wiki Markup total}} can be altered by external code independently of the {{add()}} and {{remove()}} methods. It is bad practice to expose fields from a public class \[ [Bloch 2008|AA. References#Bloch 08]\].
Compliant Solution (Private)
...
Depending on the required functionality, wrapper methods may retrieve either a reference to the HashMap, a copy of the HashMap, or a value contained by the HashMap. This compliant solution adds a wrapper method to return the value of an element given its index in the HashMap.
Exceptions
...
*OBJ01-EX0:* According to Sun's Code Conventions document \ [[Conventions 2009|AA. 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
structinstead of a class (if Java supportedstruct), then it's appropriate to make the class's instance variablespublic.
...
*OBJ01-EX1:* "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" \[ [Bloch 2008|AA. References#Bloch 08]\]. This exception applies to both mutable and immutable fields.
OBJ01-EX2: Static final fields that contain mathematical constants may be declared public.
...
CWE-766. Critical variable declared public | |
Secure Coding Guidelines for the Java Programming Language, Version 3.0 | Guideline 3-2. Define wrapper methods around modifiable internal state |
Bibliography
...
[[Bloch 2008AA. References#Bloch 08] ] | Item 13. Minimize the accessibility of classes and members; Item 14. In public classes, use accessor methods, not public fields | ]]></ac:plain-text-body></ac:structured-macro> | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="94d6e1c8-160a-4189-909a-1671e02d72a4"><ac:plain-text-body><![CDATA[ | [ [JLS 2005AA. References#JLS 05] ] | http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6] | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="70fdc2b8-4bf8-4363-b2fc-443e45a0f288"><ac:plain-text-body><![CDATA[ | |
[[Long 2005AA. References#Long 05]] | §2.2, Public Fields ]]></ac:plain-text-body></ac:structured-macro> |
...
OBJ00-J. Limit extensibility of classes and methods with invariants to trusted subclasses only 04. Object Orientation (OBJ)