Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

It is difficult to control how data members declared public or protected are accessed. Attackers can manipulate such members in unexpected ways. As a result data members must be declared private. Use wrapper accessor methods to expose class members that are to be accessed outside of the package in which their class is declared. Using wrapper methods enables appropriate monitoring and control of the modification of data members (for example, by defensive copying, validating input, and logging). The wrapper methods can preserve class invariants.

...

Code Block
bgColor#FFCCCC
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");
    }
  }
}

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#Bloch 08]\].

Compliant Solution (Private)

This compliant solution declares total as private and provides a public accessor so that the required member can be accessed beyond the current package. The add() and remove() methods modify its value without violating any class invariants.

Note that care must be taken when providing references to private mutable objects from acessor accessor methods; see rule OBJ05-J. Defensively copy private mutable class members before returning their references for details.

...

This noncompliant code example shows a static mutable hash map with public accessibility.

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

...

Mutable data members that are static must be declared private.

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

public static String getElement(int key) { 
  return hm.get(key);
}

...

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.

...

Failing to declare data members private can break defeat encapsulation.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ01-J

medium

likely

medium

P12

L1

...

Detection of public and protected data members is trivial; heuristic detection of the presence or absence of getter and setter wrapper accessor methods is straightforward. However, simply reporting all detected cases without suppressing those cases covered by the exceptions to this rule would produce excessive false positives. Sound detection and application of the exceptions to this rule is infeasible; however, heuristic techniques may be useful.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e0b0bf12c465b644-dca7940e-45bc4a2b-815fbcf2-f5daaf9fa43ce1eb6ee3f696"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#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="60e0de22143629c3-20171ea4-46024408-a79baeb2-4f03de250f8b74148c6cb79c"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#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="328e3bcac897ff1c-e0186a17-40c1421c-8619b6d1-b1d978938830fd83e6891cdb"><ac:plain-text-body><![CDATA[

[[Long 2005

AA. Bibliography#Long 05]]

§2.2, Public Fields

]]></ac:plain-text-body></ac:structured-macro>

...