Creating a Static Referance of a Class used in Singleton.In singleton you The class constructor will be of private.When constructor is private you cant directly create a instance you need to have a static method to ctreate a instance instance variables cant be used in static methods so we use static instance varible to create the class.
I think you got it
with Regards,<br />VidyaVikas Yalamanchili SCJP 5.0
i think we need to check whether the instance is null then only we have to create a new static instance is that right for a singleton??
Giovanni De Stefano
Joined: Aug 17, 2004
it depends on what your constructor does: if the constructor opens a file or worse connects to a DB or does anything that might go wrong, then yes, you have to check if the INSTANCE is null or not (and catch any exception) and if it is you should fall back to a default (sometimes dummy) alternative.
If you are not doing anything critical then I always felt comfortable skipping that check.
In my experience, with a PropertyManager Singleton, I usually had to read te properties from a file, but if the file was not there, I never returned null but instead I logged the issue and used a default PropertyManager with default values.
I believe this brings more robust code and it is a neater design.