Assuming the following class structure:
abstract class Animal<A extends Animal<A>>
{
... someAbstractMethodDefinitions
}
class Duck<D extends Duck<D>> extends Animal<D>
{
public int getBeekSize()
{
return beekSize;
}
public int getHeadSize()
{
return headSize;
}
}
class Mallard extends Duck<Mallard>
{
.. someCode
}
I then want to compare the ducks by beek size assuming I have a list of Ducks (not necessarily Mallards). In other words I want to do:
List<Duck> ducks = Arrays.asList(new Duck(), new Duck(), ...);
Collections.sort(
ducks,
Comparator.comparing(Duck::getBeekSize).reversed());
When I try to do this I get the compiler error: "Non-static method cannot be referenced in a static context". If I remove the .reversed() it works great and everything compiles and runs correctly. The following code works:
List<Duck> ducks = Arrays.asList(new Duck(), new Duck(), ...);
Collections.sort(
ducks,
Comparator.comparing(Duck::getBeekSize));
In other words why can I add a reversed() to my Comparator? The only difference between the two blocks of code is the .reversed() is removed.