1

I am using a Java Comparator to sort an ArrayList of SomeClass items. It works just fine, no problems. What I'm curious about is the syntax. Given:

 public static class PrioritizedPendingListComparator implements Comparator<SomeClass> {

        @Override
        public int compare(SomeClass o1, SomeClass o2) {
        ...
        }

And then, some time later,

private ArrayList<SomeClass> pendingList = new ArrayList<>(); 

... // fill it up with entries

Collections.sort(pendingList, new SomeClass.PrioritizedPendingListComparator());

I use new SomeClass.PrioritizedPendingListComparator() as the second argument to Collections.sort(). My comparator, PrioritizedPendingListComparator, is static so I don't expect to have to use new.

  1. Why can I not just use SomeClass.PrioritizedPendingListComparator e.g. why do I need the new?
  2. How would I know this from reading the documentation?
kmort
  • 2,848
  • 2
  • 32
  • 54
  • Explain your rationale for not having to use `new` with `static` nested types. Do you know what `new` is? What do you think it does? Why do you think so? – Sotirios Delimanolis Aug 12 '15 at 19:34
  • `static inner class` doesn't mean that this is a `static` instance of the class: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – EpicPandaForce Aug 12 '15 at 19:35
  • I think you're confusing making a class static with making a method static. It's not at all the same thing. – azurefrog Aug 12 '15 at 19:35
  • https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html – Sotirios Delimanolis Aug 12 '15 at 19:36
  • For any folks, like me, who don't have the appropriate Java vocabulary to successfully search for information on `Comparator super T> c` without ending up with non-relevant information about the ternary operator, take a look at this question: http://stackoverflow.com/questions/3009745/what-does-the-question-mark-in-java-generics-type-parameter-mean – kmort Aug 13 '15 at 13:14

1 Answers1

3

SomeClass.PrioritizedPendingListComparator is a class. You need an instance of it so you have to use new

When you define public static class the static means it doesn't implicitly have a reference to the outer class, but you still have to create it in the normal way.

One way to avoid new is to use an enum.

public enum PrioritizedPendingListComparator implements Comparator<SomeClass> {
    INSTANCE;

    @Override
    public int compare(SomeClass o1, SomeClass o2) {
    ...
    }

later

 Collections.sort(pendingList, SomeClass.PrioritizedPendingListComparator.INSTANCE);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    Singleton enum classes are awesome. – EpicPandaForce Aug 12 '15 at 19:37
  • 1
    If you don't agree with my dupe closure feel free to reopen, I'm only 95% sure. – durron597 Aug 12 '15 at 19:37
  • 3
    *Stateless* singleton enums are awesome. – Peter Lawrey Aug 12 '15 at 19:37
  • @durron597 dup is reasonable, but in this case I thought there was more to add. – Peter Lawrey Aug 12 '15 at 19:38
  • You can also import ```SomeClass.PrioritizedPendingListComparator``` and then use it without the ```SomeClass.``` prefix – Franz Becker Aug 12 '15 at 19:43
  • I guess my big question is why I need an instance. Looking at the method signature `public static void sort(List list, Comparator super T> c)` I expected to pass it a type (or template) to sort with, not an instance of a sorter. (Yes, I'm fairly new to Java...) – kmort Aug 12 '15 at 19:53
  • In other words, how do I read ` super T>`? :-) – kmort Aug 12 '15 at 19:54
  • In this sort of case, I tend to go with an anonymous inline class: `public static final FOO_COMPARATOR = new Comparator() { ...` – chrylis -cautiouslyoptimistic- Aug 12 '15 at 20:00
  • @kmort when you see `Comparator` or indeed any other type, you have to pass a reference to an instance of that type. The only time you don't do this is when you you pass a primitive. – Peter Lawrey Aug 12 '15 at 20:30
  • @kmort In short, Java only has references to instances and primitives. This is no way to pass a type without passing either an instance of that type or an instance of a `Class` which refers to that type. – Peter Lawrey Aug 12 '15 at 20:32