Hi Rammohan,
I tried to check out the design pattern's topic on the group but *I
didn't know how*. anycase, the question was about the maining
of a singleton class. well, a singleton is class that we want* to
instanciate only one time ( a Single instance) during the
excecution of an application*.
To be sur that the class have a Single instance we need to limit
access to the constructor by declaring it as *private or
protected*.
However, *declaring the constructor as protected DO NOT certify that you
have a Single instance if the class is inherited
by a subclass.
*In case we don't have Constrators, we will be obliged to look for an
other way to return a Single instance :
. In fact, we implement a *PUBLIC STATIC METHOD* called by convention *
getInstance().
Here's an example of a singleton class:
* public class YouClassName
{
private static YouClassName instance; // static instance of type
YouClassName
public static YouClassName getInstance()
{
if( instance == null) // if we the class is called for the first time
{
instance = new YouClassName();
}
return instance;
}
/*
the construcor is declared private so to prohibit calling it and to
force the call of the method getInstance
*/
private YouClassName()
{
}
}
Hope that can help, Have a nice day.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en