java - Difference between the two method references to instance methods -


import java.util.list; import java.util.function.*;  interface sometest <t> {     boolean test(t n, t m); }  class myclass <t> {     boolean mygenmeth(t x, t y)     {         boolean result = false;         // ...         return result;     } }  class timepass {     public static void main(string args[])     {         sometest <integer> mref = myclass <integer> :: mygenmeth;   //statement 1          predicate <list<string>> p = list<string> :: isempty;       //statement 2     } } 

my query

in above code, statement 1 produces 2 compile time errors

1- can not find method mygenmeth(integer, integer)

2- non static method mygenmeth(t, t) can not referenced static context

where as, statement 2 shows no error.

1- what difference between statement 1 , statement 2??

2- how statement 2 working fine.

(i not asking why statement 1 producing error).

because have method references instance methods, don't specify specific instance, instance needs passed parameter interface method.

for statement 2, can pass instance test method of predicate:

p.test(new arraylist<>()); 

but statement 1, test doesn't take instance parameter:

mref.test(new myclass<>(), 1, 2); 

to make compile, sometest needs changed to:

interface sometest<t> {     boolean test(myclass<t> instance, t n, t m); } 

alternatively, can make method references refer specific instances, interface method doesn't need contain parameter:

sometest <integer> mref = new myclass<integer>()::mygenmeth; mref.test(1, 2);  supplier<boolean> p = new arraylist<>()::isempty; p.get(); 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -