Calling the static method multiple times will return the same instance not
multiple instances, that is the essence of singleton design pattern.

Example: http://www.docjar.com/html/api/java/lang/Runtime.java.html

On Mon, Sep 5, 2011 at 10:45 PM, ravindra patel <ravindra.it...@gmail.com>wrote:

> 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 <neha.ndelhi.1...@gmail.com>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 algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> 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 algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to