Class within a class
- From: MS <matthews@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Apr 2005 13:42:01 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 .
- Follow-Ups:
- Re: Class within a class
- From: Aleksandar Pecanov
- Re: Class within a class
- Prev by Date: Re: exception inside constructors and finalize
- Next by Date: Re: Class within a class
- Previous by thread: Learning Java - How should I go about it?
- Next by thread: Re: Class within a class
- Index(es):
Relevant Pages
|