There are 3 constructs to reference the method:
object::instanceMethodClass::staticMethodClass::instanceMethod
Line:
Capitalizer c = String::toUpperCase;
use the 3'rd construct - Class::instanceMethod. In this case, the first parameter becomes the object of the method . This construction is equivalent (translated) to the following Lambda:
Capitalizer = (String x) -> x.toUpperCase();
This Lambda expression works because Lambda takes Stringas a parameter and returns the result String- as required by the interfaceCapitalizer .
Line:
c = Main::toUpperCase; //Compile error
Translated to:
(Main m) -> m.toUpperCase();
Capitalizer. , Capitalizer :
interface Capitalizer {
public String capitalize(Main name);
}
Main::toUpperCase.