简体   繁体   中英

Java 8 Method expression is giving compilation error

I am new to Java 8 (Lambda Expression). The following piece of code prints all the directories at the path indicated by the variable "file".

File file = new File("/vssexclude/Test/workspace/Test");

File[] names = file.listFiles(fileName -> fileName.isDirectory());

for (File name : names) {
    System.out.println(name.toString());
}

But, when I try to replace the lambda expression with method expression, eclipse is giving compilation error:

File[] names = file.listFiles(File::isDirectory());

What am I missing?

Remove the parenthesis :

File[] names = file.listFiles(File::isDirectory);

When you want to refer to an instance method of an object of a particular type, the syntax to use is :

ContainingType::methodName

This is described in more details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM