Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java query

  • 19-07-2006 11:03am
    #1
    Registered Users, Registered Users 2 Posts: 197 ✭✭


    File dir = new File("C:\\test");
    	    
    /*
     * This is to filter out folders starting with
     * a full stop.
     *  
     */
    FilenameFilter filter = new FilenameFilter() {
         public boolean accept(File dir, String name) {
               return !name.startsWith(".") && dir.isDirectory();
         }
    };
    	    
    	    // The list of files can also be retrieved as File objects
    	    File[] files = dir.listFiles(filter);
    

    Can someone explain the method to me please. I didnt know you could create a new object and then wrap a method in it?!?


Comments

  • Closed Accounts Posts: 162 ✭✭totoal


    Your dealing with anonymous inner classes here.
    Google it.


  • Closed Accounts Posts: 248 ✭✭comanche


    Filename filter files .. it can be used by a number of classes. Most often used in the File Chooser class (FileDialog) and java.io.File list method ...

    So what the line here are doing is setting up an instance of Filename ... Filename is an interface with one method to implement - boolean accept(File dir, String name). It is up to you to create the implementation Filename defining what files to accept so that classes like FileDialog or the method list in java.io.File can filter out files that it doesn't understand...

    at present the filter is set to list files that don't start with a dot (i.e hidden files) and directories.

    The file is passed to the file object already created pointing to C:\\test. When the list method is called on this file with the filename filter it will only return back files which are not starting with dot and are directories


  • Registered Users, Registered Users 2 Posts: 304 ✭✭PhantomBeaker


    By the way, I'm not sure if all the tutorials explain why people do this, so I'll give it a quick go at explaining WHY some people use them.

    You could take that bit of code and instead say "class DotFilenameFilter implements FilenameFiler{ /* rest of stuff goes here */ " and put it in a file. Now there are some people (like myself) who don't like putting a class in a file if you know it's probably only ever going to be used in one spot in your program.

    So you then have a choice. You can create a named private class after your public class definition... that's one way to do it. Or create a named inner class - pretty much the same thing except you have more visible to that sort of class.

    But if you're just going to be using this code once, why not just have it all inline, so that when someone's looking at your code and saying "What in the hell does DotFilenameFilter do?" they don't have to jump down. Plus, the name's not cluttering up your namespace.

    So
    FilenameFilter filter = new FilenameFilter(){ // class def here }
    

    is saying to javac "Hey, I need a class that implements this interface, but I don't really care if it has a name or not, just so long as it's implementing this interface, because that's all I'll be using it for."

    It's sorta dodgy... I don't like using them too often.If I have even the slightest suspicion I'll use it again, I'll put it in a named inner class.

    One important point is, anonymous inner classes don't have a name to the programmer, as a result you can't define constructors. So you can't pass parameters in the constructor. But if you're making something so specialised anyway, you're probably not going to want to pass it variables that way anyway.

    I find it handy if I'm cobbling together quick code, and think it's a little shiny bit of syntactic sugar, but I've never found it useful unless I KNOW it's going to be a bit of a throwaway class.


  • Closed Accounts Posts: 36 pjfogarty


    u could try using google ,its really good,you can find it at www.google.ie


  • Registered Users, Registered Users 2 Posts: 197 ✭✭Patrick


    pjfogarty wrote:
    u could try using google ,its really good,you can find it at www.google.ie

    Cheers PJ now go do some work :D


  • Advertisement
Advertisement