Jump to content
  • Advertisement
Sign in to follow this  
Omaha

[java] Object.equals()

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a neater way to implement this method for a homemade class than:
public boolean equals(Object other)
{
     if(other.getClass()==this.getClass())
     {
          // do stuff to check for equality
     }
     else
     {
          return false;
     }
}

It does the job but it seems so hacky. Java is not my language of choice.

Share this post


Link to post
Share on other sites
Advertisement

// Initialized somewhere
Sprite a;
if (a instanceof Monster) {
// Do something with the monster
} else if (a instanceof Player) {
// Do something with the player
}



And the equals() method usually checks the hash pattern to identify the equality between two objects, if I remember well.

Son Of Cain

Share this post


Link to post
Share on other sites
According to the J2SE reference on Sun's website, overriding one sort of implies that you should override the other. The implication is that if a.hashCode()=b.hashCode() then a.equals(b), but not the other way around, I think. Or maybe it's reversed.

Anyway, it suggests overriding both if you override one, and that's what I've done. Thanks for the tips.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!