I can't figure out what the point is of the 'final Person other' line. On the first conditional test a control is executed to check if the parameter isn't null ,which makes sense to me. Secondly ,if the classes are identical so a comparison can be made, which again makes sense to me . But than I'm lost because I don't understand the necessity to create another Person object and use that as a reference.
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if (!Objects.equals(this.surname, other.surname)) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
Thank you for your time :)
By declaring
other
asfinal
, it means that you can not change the value once it has been set.The reason you're casting to a
Person
object is because, the parameter of is typeObject
. That means you can get all of the methods in theObject
class. By casting it toPerson
, you can access all of the methods in thePerson
class. The same functionality could be achieved by: