The Java method System.identityHashCode(...) is returning a different value for an object when I call it internally using the this reference, compared to calling it on a variable reference of the same object.
class MyObject {
public void test(MyObject that){
LOGGER.info("this hash: {}",System.identityHashCode(this));
LOGGER.info("that hash: {}",System.identityHashCode(that));
LOGGER.info("equals: {}",this == that);
}
}
and the test...
MyObject o = new MyObject();
o.test(o);
and the output...
this hash: 263009111
that hash: 524075148
equals: false
What would cause this to happen? The real object in question is a Hibernate entity, but I've added the above test directly to the code and it's shown the same behavior in a specific scenario. Why would an object show a different identity hash code using the this keyword than it would on a reference to itself? I've also confirmed that the reference is correct by setting some fields of the object and confirming that the local fields were set to the same values. So if the reference is correct, why does identityHashCode(...) return two different values, and why does the "==" operator fail? I was under the impression that this method was specifically implemented to identify references to the same logical object?