3

Hi I got suggestion from a coverity(static tool) to use non final fields only in hashCode . Why we cannot use non final fields for generating hashCode. error id is : MUTABLE_HASH_CODE

2
  • 2
    hashcode must not be mutable. Commented Oct 18, 2011 at 10:12
  • why Hashcode should not be mutable? If the state of the object changes , then its hashcode may change depending on how significant the state changes are. The argument that it causes problems with objects in hashmap ,its up to the programmer to know the behaviour of his code and code proper safegards if necessary. Commented Oct 18, 2011 at 10:39

2 Answers 2

7

A hashCode is usually used for a Hash collection such as HashSet or HashMap. If any field changes which alters the hashCode, this will leave the collection in an invalid state.

For this reason, it is preferable that the fields used in hashCode and equals (and compareTo if you have that) only use final fields. The other option is to only use fields which don't change, but this is harder to check for and enforce.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks John,Bhaskar and Lawery . The explanations helped me a lot. Thanks
6

The idea is that if two objects are equal they must have the same hashcode. So taken to the next step you could say that an object should always have the same hashcode. Obviously, if you use non-final fields in the hashcode calculation, then the hashcode will not remain the same.

For a more concrete example, if you put an object in a hashmap and then change the object such that the hashcode changes the object in now in the wrong bucket of the hashmap. BAD!

8 Comments

This itself does not mean that nonfinal feilds should be ignored in hashcode - does it ? Imagine two different objects , which differ bacuase of diffences in values of non-final fields. In this case , the hashcode should be different between the two- this is possible only if hashcode is computed taking into accout the non-final fields. If the hashcodes for the two evaluate to same , then the performance of the HashMap will degrade.
Again, what behavior would you expect for the hashcode on a single object when the fields change? Seems that if you are using the fields as part of the equals / hashcode contract that should be locked down and to change then you create a new instance of the class with the different values.
First , Hashcode should always work in line with equals - use the same set of fields to evaluate equality or hashcode - if they dont , then its broken. Second, the requirement to lock down fields and create objets anew with changing state is still acceptable , but not the suggestion that for the sake of keeping hashcode immutable , let hashcode use only final fields, IMHO.
Well , this is definitely your own thinking , but in Java , equals when called on a object whose state undergoes changes will still and always return true, and it is incorrect to use only non-final fields in equals ( even the hashmap logic does not apply here). To quote your own example , the person after marriage ( and name change ) is indeed different from the pre-marriage person, but equal to self in the post-marriage state.
Beauty of a democracy, we don't have to agree. That's why software dev is an art. Good discussion though. Cheers. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.