title: java - compareTo method
body: <p>i have a question about compareTo method in java. So this compareTo method compares CarOwner objects and if the calling object is earlier in chronological time in comparison to the argument returns -1, if the calling object is later in chronological time in comparison to the argument returns 1, if the calling object and argument are the same in chronological time returns 0. if the argument passed in is not a CarOwner object (use instanceof or getClass to determine this) or is null, returns -1.</p>
  
  <p>and i came up with this code, but it doesnt seem like its working, anybody have any suggestion?</p>
  
  <pre><code>public int compareTo(Object o)
  {
      if ((o != null ) &amp;&amp; (o instanceof CarOwner))
      {
          CarOwner otherOwner = (CarOwner) o;
          if (otherOwner.compareTo(getYear()) &gt; 0)
              return -1;
          else if (otherOwner.compareTo(getYear()) &lt; 0)
              return 1;
          else if (otherOwner.equals(getYear()))
              if (otherOwner.compareTo(getMonth()) &gt; 0)
                  return -1;
              else if (otherOwner.compareTo(getMonth()) &lt; 0)
                  return 1;
              else if (otherOwner.equals(getMonth()))
                  return 0;
      }
      return -1;
  }
  </code></pre>