Method reference to instance method from class type vs Method reference to instance method from instance

user1169587 :

Method reference to instance method from instance

// compile successfully, out is instance, println is instance method
Consumer<String> c = System.out::println; 

Method reference to instance method from class type

// compile fail, PrintStream is class type, println is instance method
Consumer<String> c = PrintStream::println; 

why Consumer<String> c = PrintStream::println fail?

Bohemian :

But!

BiConsumer<PrintStream, String> c = PrintStream::println; // compiles!

Method references to the class have an implicit first parameter of the instance. This is similar to Method whose invoke() method’s first parameter is the instance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31657&siteId=1