Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Singleton Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton Question" Watch "Singleton Question" New topic
Author

Singleton Question

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

I ahve one doubt in Sigleton class.

For the Singleton class whether following is Mandatory:
1. Constructor should be private.
2. The object should be static.
(i.e., static classname ref = new classname(); )
3. Use static method to get the instance.

a.) Whether we can achieve the singleton class only by satisfying the above three conditions. There is any way to achieve in the same in different way.

b.) Why can't we use all the variables and method be private rather than using the constructor as private for the singleton class.

c.) Whether inheritance is possible in Singleton class(if constructor is not marked as private)
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
The singleton class has to have all it's contructors private. If you do not make them private then any client code can instantiate your singleton class. Which is something that you want to avoid.

The variable of your singleton class must also be static because the getInstance() method is static, and the getInstance() method is static because you can't create an instance of the singleton. (ie Singleton x = new Singleton() )

See singleton example below. You'll see that you have to make the construstor private so that no one else can instantiate your class. Therefore you have to make the getInstance() method public and static, so that any client doesn't need an instance of your class to exist to call the method, and that method returns the static instance of your singleton.

cheers
Darryl

public class Singleton {
private static Singleton instance;

private Singleton() {
}

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (singleton == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Sorry for not answering all your questions. Here hopefully is all the answers.

--> For the Singleton class whether following is Mandatory:
--> 1. Constructor should be private.
--> 2. The object should be static.
--> (i.e., static classname ref = new classname(); )
--> 3. Use static method to get the instance.

In my opinion they are mandatory. I haven't even thought about how else to do it, because that's really the point of using design patterns. You just implement the pattern, someone else has spent time on it finding the best way to do it, and I believe them ;-)

--> a.) Whether we can achieve the singleton class only by satisfying
--> the above three conditions. There is any way to achieve in the same
--> in different way.

You probably can, but as above why would you want to, this way works and it is a proven design.


--> b.) Why can't we use all the variables and method be private
--> rather than using the constructor as private for the singleton class.

Well if any client code can instantiate your class then it isn't a singleton. making the variables private doesn't stop someone from creating an instance of your class. It just stops them from gaining access to your variables. MyClass.variable = "test" is what making your variables private avoids.

--> c.) Whether inheritance is possible in Singleton class(if
--> constructor is not marked as private)

Inheritance is possible yes. Whether your constructor is private or not. You only inherit your super classes public and protected methods, not your super classes construstors. And if your super class is not a singleton, then anyone can instantiate your super class only, not your singleton. Your singleton will have it's own instance of its super class.


Hope this helps.

cheers
Darryl
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The Singleton pattern has a couple things going on. One is to control the number of instances. In a small note the pattern actually allows more than one - the control part is important. Another part is to provide a way to get the instance. The static getInstance() technique shown above is fine. The double check for null is overly tricky and not guaranteed to work in all situations. I'd simplify lazy creation a bit to just:

or skip the "lazy" part for even simpler

Be aware of another pattern not in the famous books that is sometimes called Just Make One. If you've done Servlet programming you might have have noticed that your servlets have public constructors but you never do a new on them. They are singletons without the control aspect, which makes them Just Make One. You are just expected to know, or discover quickly, that if you make your own instances they don't do anything useful.


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
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200

Inheritance is possible yes. Whether your constructor is private or not. You only inherit your super classes public and protected methods, not your super classes construstors. And if your super class is not a singleton, then anyone can instantiate your super class only, not your singleton. Your singleton will have it's own instance of its super class.


Correct me if I am wrong, inheritance is possible in singleton class, only if the singleton class should not be a super class. A singleton class can inherit from the super class public and protected methods.
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
BTW, it helps if you override the clone method in your Singleton class.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Inheritance is possible yes. Whether your constructor is private or not.
Perhaps I'm missing something, but if a class only has private constructors, how are you going to extend it? Can you provide a compileable example of a class that extends the following?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Correct me if I am wrong, inheritance is possible in singleton class, only if the singleton class should not be a super class.
Inheritance should not be allowed of a Singleton. It wouldn't be a Singleton if clients could instantiate a bunch of new subtypes.
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129

The Singleton pattern has several advantages over the "static class" pattern. First, a singleton can extend classes and implement interfaces, while a static class cannot (well, it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded. A sinlgeton class can be extended and it's methods overidden.
Perhaps the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance. For instance, assume you have a Configuration class that holds some global configs. Methods that use this configuration information may be defined as:

public void doSomething(Configuration config) {...}

When you start writing your system you may have only one global instance, so you make Configuration a singleton. But at some later point you may want to support more than one configuration set. Maybe you'd want to allow the user to load a Configuration object from and external file or or programmaticaly create his own (this happened in my code several times). Provided that the classes or methods that use a configuration object allow the user to pass his own instance, most of your code need not be aware of the "global" nature of the class and is therefore more flexible.


Whether inheritance is possible in Singleton. If so, please give example codings. The above quotes is taken from theserverside.com in the answer section for the question "How singleton class is differ from static class?"
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
What I meant by inheritance is possible with singleton is that the singleton can inherit from it's super class. Because your constructor is private you cannot create a class that extends your singleton. Because of the implicit call to super() in your constructor.

sorry for the misunderstanding.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are other mechanisms to control a singleton besides a private constructor. See if this makes sense:

Note the packages and visibility modifiers. The Tester class cannot see the PrivateOne class so it can never create one. Try importing and creating one to see. The Accessor can create any implementation of PublicInterface it likes. You can use inheritance freely from PrivateOne or anything else since all you have to do is implement a simple interface!

(I did a little post-testing editing to clean up a couple signatures. Lemme know if it doesn't really compile!)
[ July 28, 2005: Message edited by: Stan James ]
 
 
subject: Singleton Question