aspose file tools*
The moose likes Java in General and the fly likes Singleton object with multiple class loaders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton object with multiple class loaders" Watch "Singleton object with multiple class loaders" New topic
Author

Singleton object with multiple class loaders

Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
Hi,

I am aware of two common singleton patterns:
1) make all methods static and make constructor private.
2) keep a static reference to the singleton object, make constructor private, but expost a public static accessor to that static singleton referece.

In an environment with a single JVM but multiple classloaders, my understanding is that more than 1 class object can be created from the same fully-qualified class name and therefore it is still possible to create more than 1 instance of the class (i am referring to class declaration here, not class object) for both approaches.

What do you guys think?

Victor
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
You are correct Victor.

Regards
Maulin
Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
Maulin,

So what is the way to ensure one single instance in the JVM? By making sure such class is only loaded by the Bootstrap or System CL as opposed to any possible application/custom CL?

Thanks
Victor
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

'Promoting' the code so that it gets loaded higher in the ClassLoader hierarchy is a solution, but moving it all the way to the bootstrap or extensions is usually a bad idea. If it was a J2EE application it may be sufficient to place it in the <server installation>/common/classes or equivalent, but I'm not sure how your application is set up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton object with multiple class loaders
 
Similar Threads
Singleton Issue
Singleton in Java
Singleton Factory Object
Singleton Question
implementing singleton pattern