Is it a bad practice to include fields inaccessible by any public methods (and which themselves are not and can not be derived from any other public fields) into equals()
?
What are possible bad consequences of doing so?
Is this described as a bad practice somewhere? (I assume hash/equals contract is ok)
I feel, such equals
would be philosophically very wrong. Such equals
is not observed in the world. But can it lead to real programming misbehavior, philosophy aside?
It's absolutely fine and sometimes necessary. The only fields that you shouldn't compare are ones marked as transient
.
I'd assert that two instances are equal if and only if their serialisations are equal. That's why transient
matters and the degree of field encapsulation doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With