1

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);

3
  • 2
    I think you've got confused: B::test2 must be an unbound instance method reference to a method in the class B, so the lambda argument must be a B not a File. So you should have got a different compile error telling you that the argument is of the wrong type. Commented Jan 15, 2014 at 17:34
  • @Naftalin, thanks, I see the problem!!! However, are the conversions I wrote correct? What do you think about? Commented Jan 15, 2014 at 18:57
  • Yes, the conversions all look fine. Commented Jan 15, 2014 at 22:34

2 Answers 2

2

Short answer: Yes, your conversions are correct.

public void question() {
    File dir = new File("C:\\TEST");

    // here UNBOUND instance method reference
    dir.listFiles((File f) -> f.isFile());
    dir.listFiles(File::isFile);

    // here static method reference
    dir.listFiles((File f) -> B.test1(f));
    dir.listFiles(B::test1);

    // here BOUND instance method reference
    B b = new B();
    dir.listFiles((File f) -> b.test2(f));
    dir.listFiles(b::test2);
}

The compilation problem is because the reference;

dir.listFiles(B::test2);

or (expanded as a lambda);

dir.listFiles((pathname) -> B.test2(pathname));

is trying to access test2 as a static method. It's a static method reference. Whereas

dir.listFiles(File::isFile);        

or as a lambda;

dir.listFiles((f) -> f.isFile())

is an instance method reference, it's referring to a (non-static) method on the File class.

So although the File::isFile looks like a static reference, the use of the type in this context is actually referring to an object! Weird huh?

Sign up to request clarification or add additional context in comments.

Comments

1

I think you've got confused: B::test2 must be an unbound instance method reference to a method in the class B, so the lambda argument must be a B not a File. So you should have got a different compile error telling you that the argument is of the wrong type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.