General Java Questions I
Therefore, you should use
Single.getInstance()
to get the instance. This is (though it looks much like it) not an execution of a method
on an object, but just a method call without object. Single is only used to find out
which getInstance() method should be used, and where it is.
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."
file:///F|/a_jsite/350_tips/general_java I.htm (21 of 33) [2001 07 08 11:24:51]






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