Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Why accept() method in the interface FilenameFilter in the package java.io takes in File object ?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning File Handling in java and came across the File class.

Statement:
The File class has a method called list().
One of the overloaded methods called list takes in FilenameFilter Reference Object.

The interface FilenameFilter has a method called accept, with the signature - >  boolean accept(File dir, String name).

Question:
Since accept is an instance method called (indirectly, through FilenameFilter reference) by a File object, what is the use of File object calling a method and passing itself to a method? I see no obvious use of how this would help to filter files?

example:



The above code makes no use of File object in the accept method, especially when the File object is the same object which called the method in the first place. Can anyone suggest use or if I am missing any deeper concept. As accept method can do just fine without a File object reference.
Thanks in advance.
 
Marshal
Posts: 25183
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dinkar Joshi wrote:As accept method can do just fine without a File object reference.



No, your accept method doesn't need to use the File reference. But you're not the only one using that method. It's possible that other people might want to look at features of the directory which the file is in.
 
Paul Clapham
Marshal
Posts: 25183
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dinkar Joshi wrote:Since accept is an instance method called (indirectly, through FilenameFilter reference) by a File object, what is the use of File object calling a method and passing itself to a method?



First of all, the code inside the FilenameFilter might well need to know about the directory in question before it can say yes or no. So the most straightforward way to arrange that is to pass the directory as a parameter.

And second, I don't believe you have looked at all other places where a FilenameFilter is used in the standard API.
 
Dinkar Joshi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Dinkar Joshi wrote:Since accept is an instance method called (indirectly, through FilenameFilter reference) by a File object, what is the use of File object calling a method and passing itself to a method?



First of all, the code inside the FilenameFilter might well need to know about the directory in question before it can say yes or no. So the most straightforward way to arrange that is to pass the directory as a parameter.

And second, I don't believe you have looked at all other places where a FilenameFilter is used in the standard API.




Thank You. Your reasoning makes sense, I think I need to read and find more examples of accept() usage, hope, I would stumble upon the File object usage in those. Thanks again.
 
Paul Clapham
Marshal
Posts: 25183
64
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java API for a start. On the page for FilenameFilter (and in fact on every class and interface's page), at the top, there's a "USE" link. That will show you everything in the standard API which uses FilenameFilter. If you really feel keen, you can then look in the source code for the standard API and see how people have used that method.
 
Dinkar Joshi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Have a look at the Java API for a start. On the page for FilenameFilter (and in fact on every class and interface's page), at the top, there's a "USE" link. That will show you everything in the standard API which uses FilenameFilter. If you really feel keen, you can then look in the source code for the standard API and see how people have used that method.



Thank You for the tip. I shall.
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!