Reputation: 3377
I am comparing two objects in Java and have implemented equals and hashcode.
Though the objects are equal the comparison returns false.
I found that in the equals implementation it's printing the class of the two objects as different although they are same.
For one it prints: com.salebuild.model.TechnologyProduct
While for the other it prints: com.salebuild.model.TechnologyProduct_$$_javassist_71
So it fails in this condition in the equals method:
if (getClass() != obj.getClass())
{
return false;
}
Unable to find why it's appending this string: _$$_javassist_71
How can I overcome this? Could anyone suggest?
Upvotes: 2
Views: 124
Reputation: 11117
I would write something like that instead:
if (!(this instanceof TechnologyProduct))
{
return false;
}
Upvotes: 1
Reputation: 4588
Use instanceof
to check if both objects are of the same class.
Here is the code Eclipse offers to check object equality:
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof LdapUserDetails)) //check class here
return false;
// check fields for equality here
}
Upvotes: 3