1

I am going through an example which pulls an Array of hidden files from current directory related to method reference which is as mentioned below

  • using Anonymous inner class implementation
    File[] hiddenFiles = new File(".").listFiles(new FileFilter() {
      public boolean accept(File file) {
        return file.isHidden();
      }
    });
  • using Method reference implementation
    File[] hiddenFiles = new File(".").listFiles(File::isHidden);

My question is FileFilter interface has only one abstract method (boolean accept(File pathname)) while implementing accept method using method reference how it is valid to using boolean isHidden() in File class which has no parameters. I learnt that we can apply method reference only when parameters match with abstract method but here accept method has a parameter of type File but isHidden has no parameters. Could you please explain how it is valid.

Didier L
  • 18,905
  • 10
  • 61
  • 103
  • https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html – Seelenvirtuose Feb 16 '22 at 14:56
  • Does this answer your question? [What's the difference between instance method reference types in Java 8?](https://stackoverflow.com/questions/22516331/whats-the-difference-between-instance-method-reference-types-in-java-8) – Didier L Feb 19 '22 at 14:48

1 Answers1

1

It's Lambda expression + method reference. What you mentioned about accept method is about Lambda expression, and what you mentioned about File::isHidden is method reference.

They are 2 different things.

Your original one:

File[] hiddenFiles = new File(".").listFiles(new FileFilter() {
      public boolean accept(File file) {
        return file.isHidden();
      }
    });

Can be turned into: (Lamda expression)

File[] hiddenFiles = new File(".").listFiles(file ->  file.isHidden());

Then it can be turned into: (method reference)

File[] hiddenFiles = new File(".").listFiles(File::isHidden);
  • Does method reference parameters need not match with functional interface abstract method? – Raghavendra Feb 16 '22 at 15:09
  • @Raghavendra the first link of the lambda expression above should answer you: "One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data." –  Feb 16 '22 at 15:16
  • @Raghavendra And if you'are asking about method reference, then it's not about `accept()` method, it's about `isHidden` method, then there's no params for `isHidden` method. Cheers. –  Feb 16 '22 at 15:16
  • File.isHidden is a *non-static* method. This means it requires a File instance whose isHidden method can be called. That File object is the implied first argument in the method reference, because those are the rules established by the Java language: the target object is the implied initial parameter when matching a method reference. – VGR Feb 16 '22 at 18:52