I wanna create a single instance of my class.How can i create a single instance of a class.
+3
A:
use the singleton pattern.
Update :
What is the singleton pattern? The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object
Chathuranga Chandrasekara
2010-01-14 06:18:54
-1 obviously .. but he wan't to know HOW
lexu
2010-01-14 06:20:43
Thats why I've added a link. There is no use of repeating the same information here
Chathuranga Chandrasekara
2010-01-14 06:21:56
didn't see the link, your edit and my comment must have been asynchronous .. I've removed the -1
lexu
2010-01-14 06:23:33
Why do I need to reinvent the wheel and put all information here while having thousands of resources on web?
Chathuranga Chandrasekara
2010-01-14 06:23:38
(I want to point out that I did not downvote that, for once.)
Tom Hawtin - tackline
2010-01-14 06:24:42
sigh it claims my vote is too old ... strange since I am certain there was no link (that's why I went to look for one). My appologies, the -1 vote is undeserved!
lexu
2010-01-14 06:25:20
Even without a link, "use the singleton pattern" information would be enough to kick off his work. Then he know he want to use the singleton pattern and he can perform a search for that.. :)
Chathuranga Chandrasekara
2010-01-14 06:25:42
@ lexu : No hard feelings bro. Its my fault. I should add the link and the answer simultaneously :(
Chathuranga Chandrasekara
2010-01-14 06:26:40
thx for the edit, vote re-cast.
lexu
2010-01-14 06:32:33
+2
A:
Very basic singleton.
public class Singleton {
private static Singleton instance;
static {
instance = new Singleton();
}
private Singleton() {
// hidden constructor
}
public static Singleton getInstance() {
return instance;
}
}
Shane
2010-01-14 06:32:13
Explanation:I don think there is no way to restrict the execution of Constructor.The solution is to: make the constructor private - so that nobody can invoke it from outside.Use a static function to initialize the only instance of object - if it is NULL, then call the constructor internally.
effkay
2010-01-14 06:34:45
The only improvement I'd make to this is lazy initialization of the singleton.
Taylor Leese
2010-01-14 06:48:39
Why the static block? You can just init the instance on the same line where you declare it.@Taylor L You don't necessarily always want lazy init, that depends on the rest of the application and its bootstrapping requirements.
Adriaan Koster
2010-01-14 09:15:46
I have heard that inline instantiation can cause issues with older JVM's. You can also use a wrapping class for the instance.This could have also been lazily instantiated but like most things in Java, there are too many ways to skin the proverbial cat to list them all in a simple example :)
Shane
2010-01-14 10:45:00
Please - don't use tiny links. People don't know where the link goes unless the click it (and because of that a lot of people won't click). Type a explanation (or the pages titel), select it on the editor and create a hyperlink that contains the full url (not shortened). thx :)
Andreas_D
2010-01-14 08:27:20
+12
A:
To create a truly single instance of your class (implying a singleton at the JVM level), you should make your class a Java enum
.
public enum MyClass {
INSTANCE;
// Methods go here
}
The singleton pattern uses static state and as a result usually results in havoc when unit testing.
This is explained in Item 3 of Joshua Bloch's Effective Java.
gpampara
2010-01-14 06:38:55
To be exact the singleton will be only at classloader level, you have to control all the classloaders in a JVM to be able to guarantee singleton at JVM level
pgras
2010-01-14 09:29:35
I think you will find this is an abuse of what a enum is intended to do. An enum is for multiple related items (e.g. days of the week) so that you can have a type safe way of performing switches/comparisons etc. They are not for a single instance of a class. Singletons get complicated with different classloader structures but this is the case regardless of how you implement it.
Shane
2010-02-01 22:18:25
@Shane, refer to Effective Java. Java enums are much more than the simple enums that are available in other languages.
gpampara
2010-02-02 05:36:36
If you have a look at definition of an enumeration you will see it is a set of elements. A singleton by definition is a single element. I am fully aware that Java enums are more than C enums as I use them on a regular basis. I just think this is an abuse of the concept. Here are some Wikipedia links: http://en.wikipedia.org/wiki/Enumeration and http://en.wikipedia.org/wiki/Enumeration_(programming)
Shane
2010-02-03 00:42:48