• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic