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

[7500 byte] By [megasteoa] at [2007-11-27 4:27:39]
# 1
> I know its used again down here:> deleterThread.add(tempFile);> but you could easily get out of that.How do you intend to avoid needing a reference to the deleterThread? And would that construction actually be easier to use than the above?
paulcwa at 2007-7-12 9:36:20 > top of Java-index,Java Essentials,Java Programming...
# 2
> > but you could easily get out of that. How?
jschella at 2007-7-12 9:36:20 > top of Java-index,Java Essentials,Java Programming...
# 3

Hmmm, ok. Sorry, I got a bit mixed up first time.

What if you wrote it like this,

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class DirDeleter extends Thread

{

private static DirDeleter deleterThread;

static

{

deleterThread = new DirDeleter();

Runtime.getRuntime().addShutdownHook(deleterThread);

}

private static List<File> dirList = new ArrayList<File>();

public synchronized void add(File dir)

{

dirList.add(dir);

}

public void run()

{

synchronized (this)

{

Iterator iterator = dirList.iterator();

while (iterator.hasNext())

{

File dir = (File)iterator.next();

deleteDirectory(dir);

iterator.remove();

}

}

}

private void 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();

}

public static File createTempDir() throws IOException{

File tempFile = new File("yady", "souras rex");

if (!tempFile.mkdir())

throw new IOException();

deleterThread.add(tempFile);

return tempFile;

}

}

I'm not really interested in the code too much. I want to know when you would want to use a static member of the same class in a class like I have here.

public class DirDeleter extends Thread

{

private static DirDeleter deleterThread;

This is probably a bad example, I'm really just trying to illustrate my question.

megasteoa at 2007-7-12 9:36:20 > top of Java-index,Java Essentials,Java Programming...
# 4
I suppose the short answer is "when you're implementing a Singleton that way", although I realize that that's largely a hand-waving answer.
paulcwa at 2007-7-12 9:36:20 > top of Java-index,Java Essentials,Java Programming...
# 5

> Hmmm, ok. Sorry, I got a bit mixed up first time.

> What if you wrote it like this,

Which has nothing to do with the shut down hook.

> I'm not really interested in the code too much. I

> want to know when you would want to use a static

> member of the same class in a class like I have

> here.

In a static method that needs data.

jschella at 2007-7-12 9:36:20 > top of Java-index,Java Essentials,Java Programming...