static members
Hi,
I found a piece of code here on the forum from a while back. I understand how the code itself works, but I'm wondering if somecan explain something to me. This is the code:
//
// TempDir.java
//
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
publicclass TempDir
{
privatestatic DirDeleter deleterThread;
static
{
deleterThread =new DirDeleter();
Runtime.getRuntime().addShutdownHook(deleterThread);
}
/**
* For testing purpose
*/
publicstaticvoid main(String[] args)
{
try
{
int serial = 123;
// create a temp directory with a given name in c:\\project root-directory
File tempDir = TempDir.createNamed(""+serial,new File("c:\\project"));
// create an empty file in the temp directory.
File f =new File(tempDir,"hello.txt");
FileWriter fw =new FileWriter(f);
fw.write("hello");
fw.close();
System.out.println("Temp dir created is " + tempDir.getPath());
System.out.println("Temp dir (and all its content) will be deleted on exit");
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Creates a temp directory with a generated name (given a certain prefix) in a given directory.
* The directory (and all its content) will be destroyed on exit.
*/
publicstatic File createGeneratedName(String prefix, File directory)
throws IOException
{
File tempFile = File.createTempFile(prefix,"", directory);
if (!tempFile.delete())
thrownew IOException();
if (!tempFile.mkdir())
thrownew IOException();
deleterThread.add(tempFile);
return tempFile;
}
/**
* Creates a temp directory with a given name in a given directory.
* The directory (and all its content) will be destroyed on exit.
*/
publicstatic File createNamed(String name, File directory)
throws IOException
{
File tempFile =new File(directory, name);
if (!tempFile.mkdir())
thrownew IOException();
deleterThread.add(tempFile);
return tempFile;
}
}
//
// DirDeleter.java
//
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
publicclass DirDeleterextends Thread
{
private ArrayList dirList =new ArrayList();
publicsynchronizedvoid add(File dir)
{
dirList.add(dir);
}
publicvoid run()
{
synchronized (this)
{
Iterator iterator = dirList.iterator();
while (iterator.hasNext())
{
File dir = (File)iterator.next();
deleteDirectory(dir);
iterator.remove();
}
}
}
privatevoid deleteDirectory(File dir)
{
File[] fileArray = dir.listFiles();
if (fileArray !=null)
{
for (int i = 0; i < fileArray.length; i++)
{
if (fileArray[i].isDirectory())
deleteDirectory(fileArray[i]);
else
fileArray[i].delete();
}
}
dir.delete();
}
}
My question is why use this bit:
privatestatic DirDeleter deleterThread;
static
{
deleterThread =new DirDeleter();
Runtime.getRuntime().addShutdownHook(deleterThread);
}
why have a static member of the same class?
Why can't we do this:
static
{
Runtime.getRuntime().addShutdownHook(new DirDeleter());
}
I know its used again down here:
deleterThread.add(tempFile);
but you could easily get out of that. Like I say, I understand HOW the code works, what I want to know is why is it written this way, because I have seen this a few times before, but I guess I'm missing something,
Thanks
Ste

