...
| Wiki Markup |
|---|
As a public data member, {{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. Bibliography#BlochReferences#Bloch 08]\]. |
Compliant Solution (Private)
...
| Wiki Markup |
|---|
*OBJ01-EX0:* According to Sun's Code Conventions document \[[Conventions 2009|AA. Bibliography#ConventionsReferences#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.
| Wiki Markup |
|---|
*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. Bibliography#BlochReferences#Bloch 08]\]. This exception applies to both mutable and immutable fields. |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="99bcfdf03bf12874-fedd35b0-452a45d7-9f75a5e2-6195b14349ea771677c8c30d"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 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="e6c9d1a18fe29623-ce3ab522-464d4579-b28582c9-e22914d6c3fb328d77f5ff48"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS References#JLS 05]] | [§6.6, Access Control | 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="8e4a010b7428d004-7f53e99f-43d64295-90a385af-26123ab57c506a70145703cd"><ac:plain-text-body><![CDATA[ | [[Long 2005 | AA. Bibliography#Long References#Long 05]] | §2.2, Public Fields | ]]></ac:plain-text-body></ac:structured-macro> |
...