General Java Questions III
You could add a delete() method if you don t need the instance anymore:
public class Single{
   private static Single single;
   private Single {}
   public static Single getInstance(){
      if(single==null)
          single = new Single();
      return single;
    }
   public static delete(){
      single = null;
   }
}
The garbage collector can now remove the single object after delete() is called if
memory is needed.
  
Rijk Jan van Haaften
Dept of Computer Science, Utrecht University, The Netherlands
P.S by John: 
Doing more deeper investigation of this question I found one very good article about
this topic. Everybody who are interested can read full article here:
http://developer.java.sun.com/developer/technicalArticles/Programming/singletons/ 
For the rest of our audience shortly:
A Singleton class can be garbage collected and when 
".. a Singleton class is garbage collected and then reloaded, a new Singleton
instance is created. Any class can be garbage collected when no other object holds
reference to the class or its instances. If no object holds a reference to the  
Singleton object, then the Singleton class may disappear, later to be reloaded when
the Singleton is again needed. In that case, a new Singleton object will be created.
Any static or instance 
fields saved for the object will be lost and reinitialized. 
This problems exists in older JavaTM Virtual Machines1. JDK 1.2 VMs, in particular,
conform to a newer class garbage collection model that forbids any class in a given
classloader to be collected until all are unreferenced"
And you "... can also set your VM to have no class garbage collection ( Xnoclassgc
on the JRE 1.3, or  noclassgc on the IBM JVM). Keep in mind that if you have a
long running program that frequently reloads classes (perhaps through special class 
loaders such as the remote class loaders), you have to consider whether that could
cause a problematic buildup of garbage classes in the VM."
Also some people asked what is a Singleton and what is relation has it to Java.
Shortly a Singleton is one of classical design patterns that are used in software
development. More please read in free book here: 
http://www.patterndepot.com/put/8/JavaPatterns.htm
 Q: I study patterns now and would be lost and forget everything very soon! Help!
file:///F|/a_jsite/350_tips/general_java III.htm (6 of 9) [2001 07 08 11:24:53]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved