I prefer to use static
field for instances of classes that not store his state in fields instead anonymous-inner-classes. I think this good practice for to less memory and GC usage if method sort
(or other) call very often. But my colleague prefer to use anonymous-inner-classes for this case saying that JIT will optimize it.
class MyClass {
//non fields of class
/*access modifier*/ final static Comparator<MyClass> comparator = new Comparator<MyClass>(){
public compare(MyClass o1, MyClass o2){
//comparing logic
}
}
}
Usage example (I prefer):
List<MyClass> list = ...;
Collection.sort(list, MyClass.comparator);
Usage example (my colleague prefer):
List<MyClass> list = ...;
Collection.sort(list, new Comparator<MyClass>(){
public compare(MyClass o1, MyClass o2){
//comparing logic
}
});
1. Using anonymous-inner-classes in openJDK optimized?
2. Please, tell me good practice for this case.