Originally posted by Abid Sami:
... Why not 5? ...
Per the hashCode contract defined in
Object, "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result."
In this case, instances are equal if they are Test objects were either...
Values of a and b are respectively equal among instances (a==o.a && b==o.b), OR...Values of a and b are "switched" among instances (a==o.b && b==o.a). So for example, we might have a=1, b=2 in one instance; and a=2, b=1 in another instance. These are equal per the equals method. So a hashCode of a^b would not be acceptable, because 1^2 is not the same as 2^1.