‘equals()’ and ‘hashCode()’ are context-sensitive
hashCode() implementations should rely only on an internal object state. Making them dependent on other objects, context of use and other external conditions conflicts with the general contracts of consistency:
“For any reference values
y, multiple invocations of
trueor consistently return
“Whenever it is invoked on the same object more than once during an execution of a Java application, the
hashCodemethod must consistently return the same integer” [*]
Violating those contracts may cause all kinds of weird and unpredictable behavior.
A known example of this antipattern is
java.net.URL implementation where they are depending on information returned by a domain name server, rather than on actual stored URL data.