The strongest way to keep an invariant is to let the object never change after construction. Then it is checked once, in the constructor, and no later code can disturb it. Such an object can also be shared freely between callers, because nobody can change it under another's feet. The recipe has four parts: declare the class final, so no class can extend it to add changeable state or override its methods; make every field private and final, so it is assigned once, in the constructor; offer no setters; and copy any mutable object that enters or leaves. The table and paragraph below say exactly what final does.
The first row is the one that surprises people. final fixes the reference, not the object. With final StringBuilder sb = new StringBuilder("a"), the line sb.append("b") is legal and sb now holds ab, but sb = new StringBuilder() does not compile. An array is the same: a final int[] field cannot be pointed elsewhere, yet its cells can change. That is why the recipe needs defensive copies.
DEFENSIVE COPY RULE
stored=copy(argument),returned=copy(stored)
Needed for every field of a mutable type, such as an array. On an array, clone() returns a new array of the same length holding the same values; when the elements are primitives or immutable objects, that is a full copy. Without the first copy the caller still holds the stored object; without the second, every caller of the getter does.
String follows this recipe for everything a caller can see. The class is final. It keeps its characters in a private final array that no method changes after construction or hands out; methods that seem to change a string, such as toUpperCase(), return a String and leave the original unchanged. Two non-final fields cache the hash code, which depends only on the characters, so a caller always gets the same value. Immutability is why the hash code can be cached at all, why equal string literals in a program share one object, why a name a method has checked cannot change before it is used, and why Course could store title without a copy.