Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Interfaces

Raquel Smith
Raquel Smith
10,683 Points

How does equals(other) compare to the object passed in?

I am confused about how the equals(other) method is working here. I understand that we cast the obj Object as a Treet called other, but the if (equals(other)) statement doesn't take obj at all. How can you compare other and obj if you don't pass both to the equals() method?

For reference, the full code in the video is:

@override
public int compareTo(Object obj) {
    Treet other = (Treet) obj;
    if(equals(other)) {
        return 0;
    }
    //and then more stuff here
}

1 Answer

Equals is looking at the current instance of the Treet class vs. Object obj. If you were outside of the class, you would use

someTreet.equals(someObj)

but since you're inside the Treet class Java knows you're using equals on the current instance. You could also use

this.equals(other)

and you would get the same result.