• 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

FielNameFilter - how does accept () get called ???

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
problem statement - I want to look in a directory and list all the files in it which are .java files.
here's one way to do it ( many java books I've looked at contain very similar examples):
from Grant Palmer's "Java Programmer's Reference" (Wrox Press 2000):

My questions:
I understand that we call for a new object in class "MyFilter" to be constructed with this line "String[] fileList = fobj.list( new MyFilter("java") );
How does the accept method,in the MyFilter class, get called and run when this line is executed ???
------------------------------------------------
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The File class calls it, in code for the list(FilenameFilter) method. You can look at the source code for this method in src.zip (or src.jar in some JDK versions).
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
The File class calls it, in code for the list(FilenameFilter) method. You can look at the source code for this method in src.zip (or src.jar in some JDK versions).


Thanks Jim - I will do that.
When I looked at the jdk documentation for the class, it wasn't clear if or how that happened. I'm wondering if this is a common occurrence in Java?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by david eberhardt:
I'm wondering if this is a common occurrence in Java?


It's a reoccuring pattern in object oriented designs in general. It even has its own name: the Strategy Design Pattern (see http://c2.com/cgi/wiki?StrategyPattern).
Another prominent example from the Java API is the java.util.Comparator interface.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ilja and Jim.
I did unpack the source files for the io.* package and looked over the File class code for the list(FilenameFilter) method.
Now I see it!
This tip will help me to get used to looking at more source code that is part of the APIs that come with the JDK.
Much appreciated.
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic