touque.ca > Education Commons > Java > Resources

Programmer’s Solemn Obligation

A programmer’s solemn obligation is to ensure the integrity of an object’s state.

An object’s state has integrity when every one of the object’s fields contains a valid value.

How to ensure integrity

Every public class has a public interface and a private implementation. The only way a programmer can ensure the integrity of an object’s state is by controlling access to the object’s instance fields; this control requires that instance fields be private.

If instance fields are private, access and mutation must be provided via methods as described in the public interface. An accessor provides access to the state of an object. A mutator allows an object’s state to be modified in a way which preserves the integrity of the object.

For testing purposes, programmers should code a toString method to provide a string representation of an object’s state. In most cases, compareTo and equals methods should also be coded.

touque.ca > Education Commons > Java > Resources

[This page last updated 2020-12-23 at 12h13 Toronto local time.]