title: How to cast in the compareTo method to a Generic Class which Implements Comparable?
body: <p>I have an Arraylist with type Person. Name is a class that I have created. In my mainapp I'm creating some Name objects, setting each name-object to a person object and then adding the person objects to an ArrayList. When I try to sort the list using Collections.sort(), I get a ClassCastException. </p>
  
  <p>I can not see what the problem is.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>public class Person&lt;T&gt; implements Comparable&lt;Person&lt;T&gt;&gt; {
  
  private T name;
  
  public T getName() {
      return name;
  }
  
  public void setName(T name) {
      this.name = name;
  }
  
  @Override
  public int compareTo(Person&lt;T&gt; o) {
      return ((Person&lt;T&gt;) this.getName()).compareTo((Person&lt;T&gt;) o.getName());
  }
  
  }
  </code></pre>