• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton object with multiple class loaders

 
Victor Ho
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct Victor.

Regards
Maulin
 
Victor Ho
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic