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

Aditya Puri
Aditya Puri
1,080 Points

Need some help

When he implements the compareTo method, he decides to use object as a parameter type-

public int compareTo(Object obj)

But after that, he converts this object type into a treet type-

Treet other = (Treet) obj;

My question is- 1) Why didn't he use a treet as a parameter type first of all? That way we won't need to convert the object into a treet will we? Then why didn't he just make the input parameter a treet?

2) Also, how can we even use a treet type inside the treet class? It doesn't seem right..we are still constructing the treet class and won't the program give an error if we use a treet object in the treet class?

One more question- 3) He wrote if(equals(other)) but didn't specify what he wanted to compare other with..isn't the syntax supposed to be if(something.equals(other)) ? How will the program know what we want to compare other to?

2 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

this does not refer to the parameter passed in. It refers to the calling object, the object on which you invoke compareTo(). Lets's say you create two Treet objects:

Treet firstTreet = new Treet(
      "craigsdennis",
      "Want to be famous? Simply tweet about Java and use " +
      "the hashtag #treet. I'll use your tweet in a new " +
      "@treehouse course about data structures.",
      new Date(1421849732000L)
    );

Treet secondTreet = new Treet(
      "journeytocode",
      "@treehouse makes learning Java sooooo fun! #treet",
      new Date(1421878767000L)
    );

Then consider this code:

if (firstTreet.compareTo(secondTreet) < 0) {
    Sysytem.out.println("firstTreet was created before secondTreet");
}

In the above code firstTreet is the calling object so in the definition of compareTo() the keyword this refers to firstTreet and other refers to secondTreet.

As for your second question, casting does not create a new object.

Lastly, yes, the class definition refers to entire class file code.

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Aditya - Regarding your first question, Craig will change the parameter type to Treet in a later video.

As for using a Treet object inside the compareTo() method, that's not a problem and you won't get an error. You're not creating an instance of Treet inside the class definition, just passing an instance that is already created somewhere else to the compareTo() method.

Regarding your last question, the code:

if(equals(other))

is the same as this code:

if(this.equals(other))

, where the keyword this refers to the current Treet object.

Aditya Puri
Aditya Puri
1,080 Points

so, this refers to the parameter which will be passed to the method? What if there are more than one parameters?

also, about the 2nd question, we are downcasting an object into a treet. So that won't give any error. If, let's say we had written- Treet teet = new Treet() in the class file, then we would get an error as that will be making a new instance of the treet class in the class file itself..am I right? If I am right, then downcasting too makes a new treet regardlessly. Won't that give an error..how will the compiler use the treet if it hasn't even finished reading the class file? Is it possible because the compiler reads the code multiple times?

And lastly, does the term class definition refer to the entire class file code?