it usually works like this -
public class Test1
{
private static Test1 obj; // Static member hence singleton
private Test1() // To prevent the Compiler from creating default
constructor
{
// Do whatever initialization required
}
public static Test1 getInstance()
{
if (obj == null)
{
obj = new Test1(); // can call private constructor from
within the class
return obj;
}
// Will come here only if obj != null
return obj;
}
}
You can call Test1.getInstance to get the singleton object.
HTH,
- Ravindra
On Mon, Sep 5, 2011 at 10:15 PM, Neha Singh <[email protected]>wrote:
> Guys hv a doubt, plz clarify ..
> You mentioned that if a class has a private constructor then the object of
> that class can be created using call to a static method which internally
> calls the constructor and returns its reference. I can't understand why only
> 1 object can be created as mentioned by you. As in, we can call the static
> method multiple times and create multiple objects..
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> 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/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
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/algogeeks?hl=en.