Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

Class within a class

Thread view: 
MS - 28 Apr 2005 14:42 GMT
Hi,

I'm not sure what it is called but in Java you can define a class inside
another class.

I have a GUI (extends javax.swing.JFrame) which, among other things,
allows some file selections based on a file extension, to achieve this it
uses the File class method listFiles() taking a FilenameFilter.

FilenameFilter is an interface with a single method to define called
accept(File dir, String name). Rather than creating a new file containing
a class of one method which implements FilenameFilter I've put that class
inside my GUI class like this:

public class FileRename extends javax.swing.JFrame
{
    public FileRename()
    {
        initComponents();
    }

...lots of methods...followed by the class within the class here:

private class FNFilter implements FilenameFilter
{
    private String fileExtension;
    public FNFilter(String extension)
    {
        fileExtension = extension;
    }

    public boolean accept(File dir, String name)
    {
        File checkDir = new File(dir, name);
        if (checkDir.isDirectory() == true)
        {
            return false;
        }

        name = name.toLowerCase();
        fileExtension = fileExtension.toLowerCase();

        if (name.endsWith(fileExtension) == true)
        {
            return true;
        }
        return false;
    }
}  // End of FNFilter class

}  // End of FileRename class

Is this kind of coding generally acceptable or is it considered messy or a
bit of a hack? If so, how should it be done?

Thanks,

MS
Aleksandar Pecanov - 28 Apr 2005 16:12 GMT
It's called an inner class.
For a small help class, like the one you using, it is not a problem at
all. It's not considered hacking. Various GUI builders will use inner
classes for achieving some trivial tasks, even handing and similar.

However, you might avoid large class codes (since it is a good practice
with Java) and brake them up in lots of smaller classes based on their
functionality. I find this a much better method of doing things.

> Hi,
>
[quoted text clipped - 54 lines]
>
> MS
MS - 28 Apr 2005 17:25 GMT
Many thanks Aleksander.

Aleksandar Pecanov emailed this:
> It's called an inner class.
> For a small help class, like the one you using, it is not a problem at
[quoted text clipped - 63 lines]
>>
>>MS


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.