What is true about the following code? You may assume city and mascot are never null.
public class Main { private String city, mascot; private int numberOfPlayers; public boolean equals(Object obj) { if ( !(obj instanceof Main)) return false; Main other = (Main) obj; //from w ww .j av a 2s . c o m return (city.equals(other.city) && mascot.equals(other.mascot)); } public int hashCode() { return numberOfPlayers; } // imagine getters and setters are here }
equals()
method.hashCode()
method.equals()
and hashCode()
methods.C.
The equals()
method is correct.
You are allowed to use any business logic that you want in determining equality.
The hashCode()
method is not correct.
It violates the rule that two objects that return true for equals()
must return the same hashCode()
.
It is also a bad idea for the hash code to contain values that could change.