Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
General <strong>Java</strong> Questions III<br />
to get the instance. This is (though it looks much like it) not an execution of a method<br />
on an object, but just a method call without object. Single is only used to find out<br />
which getInstance() method should be used, and where it is.<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 '<br />
Singleton object, then the Singleton class may disappear, later to be reloaded when<br />
the Singleton is again needed. In that case, a new Singleton object will be created.<br />
Any 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 />
file:///F|/350_t/350_tips/general_java-III.htm (6 of 9) [2002-02-27 21:18:30]