Singleton Question

  • Thread starter Thread starter fred
  • Start date Start date
F

fred

Is there a way to set the singleton object isntance to null? I have tried
it and it does not work.

SingletonObject singletonObject = SingletonObject.GetInstance();
singletonObject = null;

Thanks
 
Nevermind. I am stupid. I created a static method that sets the instance
variable inside my singleton class to null.
 
The key to what happened in your code is the nature of object
references. Your first line of code gets a reference to the
SingletonObject. Your second line of code sets the reference to null.
The two lines together are the equivalent of

SingletonObject singletonObject = null;

The Singleton pattern is a pattern to make sure that there is only one
instance of a given object. The SingletonObject class internally keeps
a private static SingletonObject reference, which it passes to callers
of the static GetInstance method. The class does not allow you to have
any number of SingletonObject instances other than one.

I hope this helps.
 
Back
Top