-
Make all fields final.
-
Ensure that fields are correctly initialized by
constructor.
-
Override equals, hashCode and
toString.
-
Follow the recipies in Bloch.
-
Consider overriding compareTo.
-
Follow the recipies in Bloch.
An instance of an immutable data class is immutable if the
objects passed into the constructor never change.
Pair p1 = new Pair("dog", "cat");
StringBuilder b2 = new StringBuilder("dog");
Pair p2 = new Pair(b2, "cat");
b2.append("ma");
Pair p3 = new Pair(new StringBuilder("dog"), "cat");
Note that p1
is immutable, but p2
is mutable.
What about p3
?