int compare(Object o1, Object o2)
Compares its two arguments for order.
For compare 2 objects o1 and o2 need do something like:
MyClass o1=new MyClass();
MyClass o2=new MyClass();
if (o1.compare(o1,o2)>0) ......
Why this methtod not static?
If method was static possible like:
if (MyClass.compare(o1,o2)>0) ....
If it were static, how could it be called polymorphically? The point of
Comparatoris that you can pass an instance into something likesort… which has to then call the compare method on the comparator instance.If a class is capable of comparing one instance of itself to another, it should implement
Comparableinstead, so you’d write: