...
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
struct
instead 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="422b8ca32b04ecdf-2b4aefc7-4f864136-a0b1988d-42b49fde61fc31a20ea3399c"><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="d0a405f9bec758c8-421d3ae3-400b4820-9cf7adbe-e228a4776eeca7a57a8787e4"><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="5b66d15e7599bf6c-00673480-462a4617-81acb612-a4dd7b33adaaac9757cd386c"><ac:plain-text-body><![CDATA[ | [[Long 2005 | AA. Bibliography#Long References#Long 05]] | §2.2, Public Fields | ]]></ac:plain-text-body></ac:structured-macro> |
...