Obey the general contract when overriding equals
Must satisfy: reflexive, symmetric, transitive, consistent. There is no way to extend an instantiable class and add a value component while preserving the equals contract.
- Use the == operator to check if the argument is a reference to this object.
- Use the instanceof operator to check if the argument has the correct type.
- Cast the argument to the correct type.
- For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object.
Always override hashCode when you override equals
Always override toString
Provide programmatic access to the information contained in the value returned by toString.
Override clone judiciously
Immutable classes should never provide a clone method. The Clonable architecture is incompatible with normal use of final fields referring to mutable objects. A better approach to object copying is to provide a copy constructor or copy factory.
Consider implementing Comparable
Use the static compare methods in the boxed primitive classes or the comparator construction methods in the Comparator interface.
// Comparable with comparator construction methods
private static final Comparator<PhoneNumber> COMPARATOR = comparingInt((PhoneNumber pn) -> pn.areaCode)
.thenComparingInt(pn -> pn.prefix) .thenComparingInt(pn -> pn.lineNum);
public int compareTo(PhoneNumber pn) { return COMPARATOR.compare(this, pn);
}
Minimize the accessibility of classes and members
Make each class or member as inaccessible as possible.
In public classes,use accessor methods,not public fields
Minimize mutability
You should provide a public mutable companion class for your immutable class only once you’ve confirmed that it’s necessary to achieve satisfactory performance. e.g. StringBuilder.
Declare every field private final unless there’s a good reason to do otherwise.
Favor composition over inheritance
If you are tempted to have a class B extend a class A, ask yourself the question: Is every B really an A?
Design and document for inheritance or else prohibit it
A class to allow inheritance, constructors must not invoke overridable methods. Neither clone nor readObject may invoke an overridable method, directly or indirectly. The best solution to this problem is to prohibit subclassing in classes that are not designed and documented to be safely subclassed. We can either make the class final or make all the constructors private or package-private and to add public static factories in place of the constructors.