Song2.compareTo method is not at all called in this case. Because Song2 objects are added to ArrayList, which is not 'sorted' and hence doesn't need compareTo method implementation. However if Song2 objects were being added to a sorted collection such as TreeSet, then compareTo would have been called. In that case 'caller' will be TreeSet object, and called on Song2 objects.
Song2.compareTo method is not at all called in this case. Because Song2 objects are added to ArrayList, which is not 'sorted' and hence doesn't need compareTo method implementation. However if Song2 objects were being added to a sorted collection such as TreeSet, then compareTo would have been called. In that case 'caller' will be TreeSet object, and called on Song2 objects.
I m afraid. compareTo () method is called when Collections.sort() is called .
Here ArrayList contains Song2 objects which implements comparable interface. Collections.sort() will actually compareTo() method to sort it .
Collections.sort is not the only place which can trigger compareTo call. In the code below compareTo is called without Collections.sort. In a sorted collection while trying to insert a new item a suitable place is to determined by comparing the new item with existing items.
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...