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
Tonnie Fanadez
UX Design Techdegree Graduate 22,769 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