Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / August 2003

Tip: Looking for answers? Try searching our database.

Risk of static singleton classes being unloaded by the ClassLoader?

Thread view: 
David M. Karr - 22 Aug 2003 20:18 GMT
When implementing a Singleton pattern, I sometimes consider whether to
use the classic "getInstance()" concept, or simply implement a "static
utility" class, with all static methods.  In most cases, the static
utility class fulfills all of my needs.

However, I've been told (which I find hard to believe) that a class
with all static methods has a risk of being unloaded by the
ClassLoader, supposedly because you end up with no permanent
references to any part of the class.

Is there any truth to this?
A. Bolmarcich - 22 Aug 2003 22:19 GMT
> When implementing a Singleton pattern, I sometimes consider whether to
> use the classic "getInstance()" concept, or simply implement a "static
[quoted text clipped - 7 lines]
>
> Is there any truth to this?

It was true with the JDK 1.0.  The JDK 1.1 java command had a -noclassgc
option that prevented classes from being gargage collected.  Starting
with JDK 1.2 a class may not be garbage collected until after the
ClassLoader that loaded the class may be.  Starting with JDK 1.2
the java command has a -Xnoclassgc option that disables class garbage
collection.

No matter what JVM you use, you can prevent a class from being garbage
collected by keeping a reference to the class in another class or object
that will not be garbage collected.
Stephen Kellett - 23 Aug 2003 00:37 GMT
>> When implementing a Singleton pattern, I sometimes consider whether to
>> use the classic "getInstance()" concept, or simply implement a "static
[quoted text clipped - 7 lines]
>>
>> Is there any truth to this?

Even if the class is unloaded, at the point at which a call is made to a
static method, the class will be loaded. If you want to avoid any such
problems, always ensure that you have one reference in use.
Signature

Stephen Kellett
Object Media Limited    http://www.objmedia.demon.co.uk
RSI Information:        http://www.objmedia.demon.co.uk/rsi.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.