I dont know the way you want your Book class to work....
but i am telling the way how to use the compareTo()
& your error is simple , all our friends already explained.
you need here two things
1) how to use equals()
2) intention of compareTo()
for equals() go through Thinking in Java ed.4...
now for the CompareTo() the implementation must be as follows.
let b1 & b2 are your book instances
1) if b1.equals(b2) is true then b1.compareTo(b2)==0 must be true...
2) if b1.equals(b2) is false then b1.compareTo(b2) must return a non-zero...
3) any_book_obj.compareTo(null) must throw NullPointerException.
4) for incompatiable class it must throw ClassCastException.
thank you ravi
I wanted to sort the Book objects according to the year.Thats why I used compareTo in Book claas.After making year an Integer instead of a primitive the code worked.
Is there any other means of getting it done that is better?(to be used with sort())