27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

General <strong>Java</strong> Questions I<br />

You could add a delete() method if you don't need the instance anymore:<br />

public class Single{<br />

private static Single single;<br />

private Single {}<br />

public static Single getInstance(){<br />

if(single==null)<br />

single = new Single();<br />

return single;<br />

}<br />

}<br />

public static delete(){<br />

single = null;<br />

}<br />

The garbage collector can now remove the single object after delete() is called if<br />

memory is needed.<br />

--<br />

Rijk-Jan van Haaften<br />

Dept of Computer Science, Utrecht University, The Netherlands<br />

P.S by John:<br />

Doing more deeper investigation of this question I found one very good article about<br />

this topic. Everybody who are interested can read full article here:<br />

http://developer.java.sun.com/developer/technicalArticles/Programming/singletons/<br />

For the rest of our audience shortly:<br />

A Singleton class can be garbage collected and when<br />

".. a Singleton class is garbage-collected and then reloaded, a new Singleton<br />

instance is created. Any class can be garbage-collected when no other object holds<br />

reference to the class or its instances. If no object holds a reference to the ' Singleton<br />

object, then the Singleton class may disappear, later to be reloaded when the<br />

Singleton is again needed. In that case, a new Singleton object will be created. Any<br />

static or instance<br />

fields saved for the object will be lost and reinitialized.<br />

This problems exists in older <strong>Java</strong>TM Virtual Machines1. JDK 1.2 VMs, in particular,<br />

conform to a newer class garbage collection model that forbids any class in a given<br />

classloader to be collected until all are unreferenced"<br />

And you "... can also set your VM to have no class garbage collection (-Xnoclassgc<br />

on the JRE 1.3, or -noclassgc on the IBM JVM). Keep in mind that if you have a<br />

long-running program that frequently reloads classes (perhaps through special class<br />

loaders such as the remote class loaders), you have to consider whether that could<br />

cause a problematic buildup of garbage classes in the VM."<br />

Also some people asked what is a Singleton and what is relation has it to <strong>Java</strong>.<br />

Shortly a Singleton is one of classical design patterns that are used in software<br />

development. More please read in free book here:<br />

http://www.patterndepot.com/put/8/<strong>Java</strong>Patterns.htm<br />

Q: I study patterns now and would be lost and forget everything very soon! Help!<br />

file:///F|/350_t/350_tips/general_java-I.htm (20 of 31) [2002-02-27 21:18:17]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!