pick a hard-coded, randomly chosen, non-zero, odd number // ideally different for each class return new HashCodeBuilder(17, 37). append(name). append(age). append(smoker). toHashCode(); } }
Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionHashCode, uses Field.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set. It is also slower than testing explicitly.
A typical invocation for this method would look like:
public boolean hashCode(Object o) { return HashCodeBuilder.reflectionHashCode(this); }
@author
Stephen Colebourne
@version $Id: HashCodeBuilder.java 517121 2007-03-12 07:45:49Z ate $