Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 52: Singletons vs. class (un)loading

Find out how Java 1.2 changes the way we load and unload classes in Java

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

Singletons in Java 1.2

After a good bit of debate on the 'Net, combined with a number of complaints, the powers that be at Sun decided to clear up the problem by slightly modifying the Java language specification.

The new rules for determining when a class may or may not be unloaded are wonderfully simple:

  • All classes loaded through the local, system classloader will never be unloaded.

  • All classes loaded through other classloaders will be unloaded only when the classloader is unloaded.


So, for the vast majority of developers, this remedy gets rid of a relatively subtle, but annoying problem -- and we never have to lift a finger!

Dynamic loading and unloading of classes

For those of you who need to deal with loading and unloading classes on the fly, these new rules makes life a little clearer. Just load classes that you wish to later reload in your own classloader. When you want to reload the classes, nuke the classloader and then load the class through a newly created classloader.

Alas, there is not, at this time, any way to use classloaders in a secure environment from within an untrusted applet (as an untrusted applet is prevented from implementing classloaders). A RFE (um, er, request for enhancement) has been filed with Sun to create a constrained, untrusted classloader for use in untrusted applets.

About the author

Alternately as employee, consultant, and principal of his own company, John D. Mitchell has invested the last ten years in developing cutting-edge computer software and advising other developers. John co-authored Making Sense of Java: A Guide for Managers and the Rest of Us and Dummies 101: Java Programming and has published articles in programming journals. In addition to writing the Java Tips column for JavaWorld, he moderates the comp.lang.tcl.announce and comp.binaries.geos newsgroups. Bill Foote is a staff engineer with JavaSoft. He has worked on HTML browser products, low-memory handing, applet and classloader issues, security and PersonalJava. When he's not working, Bill's weapon of choice is the foil, although he plans to try the �p�e sometime in the future.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Java In-Depth columnist Chuck McManis provides a detailed look into class loaders in his October 1996 column "The basics of Java class loaders" http://www.javaworld.com/jw-10-1996/jw-10-indepth.html
  • The Patterns home page is a good source for information about all aspects of software patterns and pattern languages http://hillside.net/patterns/patterns.html