The hashCode contract basically says that if two objects are equal
as defined by the equals(Object) method, then they need to return the same hashCode value.
See hashCode method under the Object API:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html Note that
unequal objects are
not required to return different hashCode values, although the hashing would be more efficient if they did.
In this question, the equals(Object) method is defined to return true if the objects being compared are both instances of class A and they have the same value for int i1. Note that the value of int i2 has nothing to do with this definition of equals.
Therefore, the only variable that can be used to calculate class A's hashCode function is i1. The variable i2
cannot be used, because then instances of class A that are equal (as defined by the equals method) might not return the same hashCode, and the hashCode contract would be broken. This eliminates options c and d, because they both use i2.
Option a is a poor hashCode method because a constant fails to differentiate between different objects, but it
does comply with the hashCode contract: Equal objects will return the same hashCode.
[ October 18, 2004: Message edited by: marc weber ]