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 trialLucas Frixione
1,332 Pointsthis is the hardest lesson yet...
okay so this is confussing me quite a lot, what i don't understand are the following lines
public int compareTo(Object obj){
Treet other = (Treet) obj;
if (equals(other)){
return 0;
What is the parameter i'm passing? (first line) isnt it a Treet object? If so, why do i need to make another Treet object (second line) , in the third line what am i comparing to the "other" Treet object i just made?
then he does this
int dateCmp = mCreationDate.compareTo(other.mCreationDate);
is it possible to call a method inside the same method?
Anddd final question. maybe the most important one, we are creating this method in order to make the Treets comparable with Arrays.sort(treets)?
1 Answer
andren
28,558 PointsAbout your first question:
Yes the object being passed in is a treat, but it's being passed in as a generic object, in order to have access to Treet specific properties and methods you have to cast the object you get passed in to a Treet. You are essentially just telling Java to take the generic object passed in and to treat it as it it were a Treet object (since in reality that is what it is).
About your second question:
Pretty much the only time you can call a method without specifying what object or class it belongs to is when the method in question exists within the class you are writing the code in. As such in the third line you are calling the equals method that exist in the Treet class, and passing in the other Treet as a parameter.
Now You might wonder where that equals methods comes from since you never wrote it yourself, the answer is that it comes from the Object class. All classes in Java inherit from the Object class, and one of the methods the Object class has is equals. As such all classes in Java has an equals method even if they never explicitly define one.
If you don't override the equals method then by default it will simply compare the current object it is a part of against the object that is passed in. So in the third line you are comparing the current Treet against the passed in Treet.
About your third question:
Actually yes, as "wrong" as it might seem you can call a method from within said method's own definition. Doing so is perfectly fine as long as there is some conditional logic within the method that will ensure that the call to the method itself is only executed under certain circumstances. If that is not the case then you end up with an infinite loop.
But it should be noted that in the code you provide you are not actually calling a method within it's own definition. Remember that you are creating a compareTo method specifically for the Treet class. mCreationDate is not a Treet object, it is a Date object. And Date is a class which has it's own compareTo method that is completely independent of the Treet class's compareTo method.
About your fourth question:
Yes, you are implementing the comparable interface in order to make your class compatible with Array.sort, in order for Array.sort to sort objects the class the object is an instance of needs to implement the comparable interface. That is necessary because Array.sort depends on the object having a compareTo method that adheres to the specs of the comparable interface's compareTo method. If the object did not have that then it would have no idea how it was meant to sort the object.