Question

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 :)

Était-ce utile?

La solution

By declaring other as final, 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 type Object. That means you can get all of the methods in the Object class. By casting it to Person, you can access all of the methods in the Person class. The same functionality could be achieved by:

((Person)obj).personMethod();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top