// This rule
specifies whether public fields are allowed
// or not.
// In general no class should expose its internal data
// structures. This is also called 'Information Hiding'.
// You should provide get- and set methods to access value
// of data elements but not the data element itself.
// Later changes of the type are very expensive if not
// impossible. This rule applies for both isntance and
// class (static) fields. Public constants (static final)
// are allowed.
// Public fields exposing the data type
public class Coordinate
{
public static int count = 0; // count of all instances
public int x;
public int y;
}
// Data type hidden; access with methods
// You can easily exchange the data type
// if needed.
public class Coordinate
{
// old version
// private static int count = 0;
// private int x;
// private int y;
public void incrementCount() {count++;}
public int getCount() { return count; }
/** @deprecated */
public void setX(int x) {this.x = x;}
/** @deprecated */
public int getX() {return (int) this.x;}
/** @deprecated */
public void setY(int y) {this.y = y;}
/** @deprecated */
public int getY() {return (int) this.y;}
// new version
private double x;
private double y;
public void
setX(double x) {this.x = x;}
public double
getX() {return this.x;}
public void setY(double y) {this.y = y;}
public double
getY() {return this.y;}
}