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 Exploring the Java Collection Framework Upgrade Comparable to use Generics

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Help needed with generics ...

Why we have to delete the line

    BlogPost other = (BlogPost) obj;

in the challange

  public int compareTo(BlogPost other) {
    BlogPost other = (BlogPost) obj;
    if (equals(other)) {
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

I know that we give the parameter BlogPost "other" to the method compareTo() . But why we don´t need to initialise the "other" object of BlogPost class anymore???

1 Answer

Allan Clark
Allan Clark
10,810 Points

There is no need to redeclare nor reinitialize any parameters inside that method. What ever is passed to the method though the method call is accessed using the parameter. The line:

BlogPost other = (BlogPost) obj;

is only needed for casting from the Object class to the BlogPost class. Generics make it so we don't have to do this cumbersome changing of classes.

Hope this helps, let me know if i can clear anything else up.