A class invariant is a logical statement about the instantaneous state of an object that is ensured by the constructors and preserved by the methods. Let’s break that down:

  • A logic statement is a boolean
  • The instantaneous state of an object is the combination of all its value fields at some point
  • invariants are ensured by constructors sch that, whenever the constructor returns, the logical statement holds
  • maintaining the logical statement means that a good state object will always remain in a good state