| Author |
pg no 555-K and B hashCode()
|
kiruthigha rajan
Ranch Hand
Joined: Dec 29, 2011
Posts: 69
|
|
class SaveMe implements Serializable{
transient int x;
int y;
SaveMe(int xVal, int yVal) {
x = xVal;
y = yVal;
}
public int hashCode() {
return (x ^ y); // Legal, but not correct to
// use a transient variable
}
public boolean equals(Object o) {
SaveMe test = (SaveMe)o;
if (test.y == y && test.x == x) { // Legal, not correct
return true;
} else {
return false;
}
}
}
my doubt is
test.y refers to which y
and test.x refers to which x
please explain in detail
thanks in advance
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
Hello kiruthigha rajan,
Please UseCodeTags.
As you can see, Object o has been casted to SaveMe. So, test.x is x of that object (o) and test.y is y of it (o).
I hope this helps.
By the way is this the code from Sierra's book? I wonder why there is no instanceof test in equals method.
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
 |
|
|
subject: pg no 555-K and B hashCode()
|
|
|