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 Organizing Data Interfaces

I have 2 questions about the compareTo() method in the Interface video

in the method: compareTo (Object obj) { Treet other = (Treet) obj; if (equals(other)) { return 0; } int dateComp = mCreationDate.compareTo(other.mCreationDate); if (dateComp == 0) { return mDescription.compareToIgnorecase(other.mDescription); } return dateComp; }

question 1: how are you able to use a method (compareTo) inside it's very own implementation as was done to initialize dateComp variable. How was the compiler able to resolve that expression in other to assign a value to the dateComp variable? I'm confused because we are using a method inside it's very own definition.

question 2: I'm confused about the use of equals method in the if condition. what would the fully qualified version of that expression be other.equals(other)? I'm just confused by the expression: if (equals(other))

Thanks in advance!

2 Answers

I just realized that the Date class implements comparable interface hence Dates can be compared even without overriding the compareTo method.

...I'm still confused about the second question. Thanks!

Adam Goddard
Adam Goddard
3,337 Points

I think equals(other) is the same as putting this.equals(other)

With instance variables you can write this.title = "The Title"; or just title = "The Title"; and I believe the same is true with methods, it defaults to the object calling the method

I hope that's clear, if not I can put an example

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

So the Class Treet has 3 attributes i.e. mAuthor, mDescription and mCreationDate. My Question is which attribute does equals(other) compare among the 3 attributes. Thanks