If I have:
class B
{
public static boolean test1(File f)
{
return true;
}
public boolean test2(File f)
{
return true;
}
}
are the following conversions to a full lambda expressions correct?
File dir = new File("C:\\TEST");
// here UNBOUND instance method reference
// converted as? dir.listFiles((File f) -> f.isFile());
dir.listFiles(File::isFile);
// here static method reference
// converted as? dir.listFiles((File f) -> B.test1(f));
dir.listFiles(B::test1);
// here BOUND instance method reference
// converted as? dir.listFiles((File f) -> b.test2(f));
B b = new B();
dir.listFiles(b::test2);
then, still, one question:
if I write: dir.listFiles(B::test2); I have from the compiler:
non-static method test2(File) cannot be referenced from a static context
but why is that error not throws for: dir.listFiles(File::isFile);
B::test2must be an unbound instance method reference to a method in the classB, so the lambda argument must be aBnot aFile. So you should have got a different compile error telling you that the argument is of the wrong type.