public class Test { private Integer id = null; public Test() { } public int compareTo(Test test) { System.out.println("in compareTo"); return id != null ? id.compareTo(test.id) : test != null ? test.compareTo(this) : 0; } public static void main(String[] args) { Test test = new Test(); Test test2 = new Test(); test.compareTo(test2); } }