aspose file tools*
The moose likes Beginning Java and the fly likes Singelton Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singelton Class" Watch "Singelton Class" New topic
Author

Singelton Class

Ram Adsumalli
Ranch Hand

Joined: Oct 16, 2006
Posts: 64



By seeing the above code my understanding is like this. If I am wrong please Correct me

a) By providing the static reference, means the instance created can be accessed globally

My question is the purpose of Singelton Class is to create only one instance.

I hope if I am right For this we need to provide our Class Constructor as Private. So that Other Classes Can't create an instance. By looking the above code means

If the constructor is protected then we can create an instance for the class which extends ClassicSingleton Class from the same package

Please make me Clear on the above topic

Thanks in advance
Ram
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

In the example you show I think it is indeed wrong to make the constructor protected instead of private.

By making it protected, the class can be instantiated in a subclass or in any other class in the same package (also classes in the same package that are not subclasses!). The class itself does not have complete control over instantiation this way, so this is not a good way to implement a Singleton.
[ February 26, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, you got the ideas correct. As described above, you probably need a private constructor to keep things safe.

The null check you showed is a risk in multi-threaded apps. You can synchronize the method to make it safer, or just create the instance when you declare it. With both of these changes ...

This makes the class a factory for its single instance. There are other ways to implement Singleton but this is the most common.

I'll attach the usual warning that Singleton invites a variety of problems into your code. It's global which is usually bad, impossible to extend because of the private constructor, difficult to replace because other classes are tightly bound to the class name, it really assures one instance per class loader instead of one instance per JVM, and more. Any time you're tempted to use one, look for alternatives. Sometimes Singleton is the right answer, but it is overused.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Stan, please could you explain
assures one instance per class loader instead of one instance per JVM

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

My understanding is that a single JVM can have more than one class loader. This pattern only guarantees one instance per class loader, so it's still possible to have more than one instance in the JVM of the singleton class. which is exactly what you don't want.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
thanks Fred, I guess I need to do some independent research as well.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Would this help


[My Blog] [JavaRanch Journal]
Ram Adsumalli
Ranch Hand

Joined: Oct 16, 2006
Posts: 64
Hi Stan

This makes the class a factory for its single instance

If Possible can you explain more on this sentence.

Thanks for your response guys

Ram
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I call it a factory because it creates and provides access to the single instance. I usually add it's a "lower case factory" not one of the formally defined Something Factory patterns. I grew up calling generic creational utilities like that factories well before I saw GoF. Others may choose to call it something else.
 
wood burning stoves
 
subject: Singelton Class