Java interface/ Comparator

java

Solution

This code is using a feature of Java called anonymous inner classes. You specify the interface or superclass to implement/extend, along with an anonymous class body. Your anonymous inner class implements `Comparator<Fruit>`.

Problem

I am new to java and trying to understand some concepts. Here is a piece of code I don't understand. ``` public static Comparator<Fruit> FruitNameComparator = new Comparator<Fruit>() { public int compare(Fruit fruit1, Fruit fruit2) { return fruit1.quantity - fruit2.quantity; } }; ``` I know what this is doing, but can't understand why this is allowed. So my questions are: - From the java doc, Comparator[T] is an interface. How about Comparator[Fruit]? I will suppose that it is a class, because it has to override the compare function. - Why can FruitNameComparator be intialized with a non-parameter constuctor and a class definition within the {}? I didn't find such constructor declaration in javadoc of Comparator[T]. Any input will be appreciated.

Original source