• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton Class  RSS feed

 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i reload a singleton class once the instance has been created.
 
rom chatterjee
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you need to? The question being if you think of a well known singleton class eg. Math. It has no state, therefore theres no reason for having different instances flying around, and theres no need to reload it.
What does your singleton do
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to Java Ranch!
Well, singleton classes are usually designed to make this hard! There are tricks you can play with class loaders -- a class can be garbage collected when the class loader that loaded it is collected -- but this is really outside the Java language itself and subject to external control (i.e., switches passed to the JVM.)
Why don't you tell us in a bit more concrete detail what you need to do?
[Note to Rom Chaterjee: Math isn't a singleton; it's just a class with all static methods and no public constructor. I don't know if there's a name for such a class (a Zero-ton?), but it's not a Singleton; a Singleton class is a class of which one and only one instance can exist.]
[ August 27, 2003: Message edited by: Ernest Friedman-Hill ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Singleton Pattern, typically the Class has a static reference to the Singleton Object instanciated. If you wish to replace that, have a method such as refresh which will create a new instance and replace the one referenced by the class.
Now, if you aren't writting the Singleton and the author didn't provide this access, you can, if SecurityPermissins allow, use reflection to create a newInstance and directly set the reference. SecurityPermissions must be granted for you to set the ReflectPermission to suppressAccessChecks. Please note the warning with this SecurityPermission, it's not one to use lightly.
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for the reply guys.
Here is my actual problem. My singleton class fetches some data from the database. But the same database is also updated by some other application each week and that application informs my application of the updation so that i can again reload my singleton class. I hope that i was able to explain my problem.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, what Carl said about a "refresh" method should be enough, yes?
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite clear. Obviously you are wrting the Singleton class so, don't worry about reflection. How is the other application notifying your application of the update?
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx again....
i think u guys r right....that is the only way out
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!