tags:

views:

420

answers:

4

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.

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
-1 obviously .. but he wan't to know HOW
lexu
Thats why I've added a link. There is no use of repeating the same information here
Chathuranga Chandrasekara
didn't see the link, your edit and my comment must have been asynchronous .. I've removed the -1
lexu
Why do I need to reinvent the wheel and put all information here while having thousands of resources on web?
Chathuranga Chandrasekara
(I want to point out that I did not downvote that, for once.)
Tom Hawtin - tackline
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
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
@ lexu : No hard feelings bro. Its my fault. I should add the link and the answer simultaneously :(
Chathuranga Chandrasekara
thx for the edit, vote re-cast.
lexu
+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
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
The only improvement I'd make to this is lazy initialization of the singleton.
Taylor Leese
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
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
A: 

Follow the link will be useful http://tiny.cc/7Onm3

Grayskull
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
Considered.....will follow from next time ;)
Grayskull
+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
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
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
@Shane, refer to Effective Java. Java enums are much more than the simple enums that are available in other languages.
gpampara
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