How does this code do what it does?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How does this code do what it does?

  1. #1
    Paul Salvaggio Guest

    How does this code do what it does?


    This IO example is taken from Chapter 11 of Bruce Eckel's FREE online book
    called 'Thinking in Java' (http://www.MindView.net). In reference to the
    DirectoryFilter class in the example below, Bruce explains: "The whole reason
    behind the creation of this class is to provide the accept( ) method to the
    list( ) method so that list( ) can call back accept( ) to determine which
    file names should be included in the list. Thus, this technique is often
    referred to as a callback or sometimes a functor (that is, DirFilter is a
    functor because its only job is to hold a method)."

    Questions: I am confused as to how the code below actually calls the method
    named 'accept'. I was under the impression that in order to run a method
    from a class other than a constructor, you had to physically call it, for
    example: DirFilter.accept(arg1,arg2). Where, when and how is the accept()
    method called? What am I not understanding?

    //: c11irList.java
    // Displays directory listing.
    import java.io.*;
    import java.util.*;
    import com.bruceeckel.util.*;

    public class DirList {
    public static void main(String[] args) {
    try {
    File path = new File(".");
    String[] list;
    if(args.length == 0)
    list = path.list();
    else
    list = path.list(new DirFilter(args[0]));
    Arrays.sort(list,
    new AlphabeticComparator());
    for(int i = 0; i < list.length; i++)
    System.out.println(list[i]);
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    }

    class DirFilter implements FilenameFilter {
    String afn;
    DirFilter(String afn) { this.afn = afn; }
    public boolean accept(File dir, String name) {
    // Strip path information:
    String f = new File(name).getName();
    return f.indexOf(afn) != -1;
    }
    } ///:~


  2. #2
    Paul Clapham Guest

    Re: How does this code do what it does?

    In this line:

    list = path.list(new DirFilter(args[0]));

    Object "path"'s method "list" is called, with a parameter that is a new
    DirFilter object. As far as "list" is concerned, it receives an object that
    implements FilenameFilter, which is a standard java.io interface that must
    implement the "accept" method. We don't see the code here, but we can
    assume that "list" is going to call "accept" -- what else could it do with
    that parameter?

    PC2

    Paul Salvaggio <standingo@yahoo.com> wrote in message
    news:39cd01d0$1@news.devx.com...
    >
    > This IO example is taken from Chapter 11 of Bruce Eckel's FREE online book
    > called 'Thinking in Java' (http://www.MindView.net). In reference to the
    > DirectoryFilter class in the example below, Bruce explains: "The whole

    reason
    > behind the creation of this class is to provide the accept( ) method to

    the
    > list( ) method so that list( ) can call back accept( ) to determine which
    > file names should be included in the list. Thus, this technique is often
    > referred to as a callback or sometimes a functor (that is, DirFilter is a
    > functor because its only job is to hold a method)."
    >
    > Questions: I am confused as to how the code below actually calls the

    method
    > named 'accept'. I was under the impression that in order to run a method
    > from a class other than a constructor, you had to physically call it, for
    > example: DirFilter.accept(arg1,arg2). Where, when and how is the accept()
    > method called? What am I not understanding?
    >
    > file://: c11irList.java
    > // Displays directory listing.
    > import java.io.*;
    > import java.util.*;
    > import com.bruceeckel.util.*;
    >
    > public class DirList {
    > public static void main(String[] args) {
    > try {
    > File path = new File(".");
    > String[] list;
    > if(args.length == 0)
    > list = path.list();
    > else
    > list = path.list(new DirFilter(args[0]));
    > Arrays.sort(list,
    > new AlphabeticComparator());
    > for(int i = 0; i < list.length; i++)
    > System.out.println(list[i]);
    > } catch(Exception e) {
    > e.printStackTrace();
    > }
    > }
    > }
    >
    > class DirFilter implements FilenameFilter {
    > String afn;
    > DirFilter(String afn) { this.afn = afn; }
    > public boolean accept(File dir, String name) {
    > // Strip path information:
    > String f = new File(name).getName();
    > return f.indexOf(afn) != -1;
    > }
    > } ///:~
    >




  3. #3
    Vikram Rajan Guest

    Re: How does this code do what it does?


    hi,

    I agree with Paul Clapham. The only conceivable place where accept could
    be called is inside the list method.

    Vikram


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center