Hi, so I have a question about this code. It says that if myID is equal to "t.getID" then return 0. Otherwise, it uses the "compareTo" method, within the definition of a compareTo method. How can a method be in the definition of itself?
public class TestClass implements Comparable<TestClass> { private String myName; private int myID; public TestClass( String name, int id ) { myName = name; myID = id; } public String getName() { return myName; } public int getID() { return myID; } //This section is where the editable code is. But I didn't write it. It was there to start me off I guess.: public int compareTo( TestClass t ) { if ( myID == t.getID() ) return 0; return myName.compareTo( t.getName() ); } //That was the first part of editable code //That was the part that was mysterious to me. } public class MainClass { public static void main( String[] args ) { TestClass c1 = new TestClass( "zulima", 1 ); TestClass c2 = new TestClass( "fred", 2 ); TestClass c3 = new TestClass( "eric", 3 ); TestClass c4 = new TestClass( "jane", 2 ); TestClass c5 = new TestClass( "hermione", 1 ); //I can also edit this line right here: fSystem.out.println( c1.compareTo( c2 ) ); } }
I'm also looking for a good explanation of what the "compareTo" method is trying to do, other than return 0. I mean, I know its supposed to compare but, how does it compare conceptually.
Thanks for the help. Sincerely, ghostheadx