9

I want to create only one object of an class and reuse the same object over and over again. Is there any efficient way to do this.

How can I do this?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • What exactly are you trying to do? Do you have an example class? – cowls Jan 03 '13 at 08:42
  • @AjayGopalShrestha if the question has been answered to your satisfaction, please mark the appropriate answer as accepted. – Steve Jan 03 '13 at 08:59

8 Answers8

11
public final class MySingleton {
    private static volatile MySingleton instance;

    private MySingleton() {
        // TODO: Initialize
        // ...
    }

    /**
      * Get the only instance of this class.
      *
      * @return the single instance.
      */
    public static MySingleton getInstance() {
        if (instance == null) {
            synchronized (MySingleton.class) {
                if (instance == null) {
                    instance = new MySingleton();
                }
            }
        }
        return instance;
    }
}
Steve
  • 8,066
  • 11
  • 70
  • 112
2

This is generally implemented with the Singleton pattern but the situations where it is actually required are quite rare and this is not an innocuous decision.

You should consider alternative solutions before making a decision.

This other post about why static variables can be evil is also an interesting read (a singleton is a static variable).

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
2

The simplest way to create a class with one one instance is to use an enum

public enum Singleton {
    INSTANCE
}

You can compare this with Steve Taylor's answer to see how much simple it is than alternatives.

BTW: I would only suggest you use stateless singletons. If you want stateful singletons you are better off using dependency injection.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

That would be the Singleton pattern - basically you prevent construction with a private constructor and "get" the instance with a static synchronized getter that will create the single instance, if it doesn't exist already.

Cheers,

Community
  • 1
  • 1
Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
0

There is a design pattern called Singleton. If you implement it, you will get to what you need.

Take a look at this to see different ways to implement it.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

Yes. Its called a Singleton class. You create one and only instance and use it throughout your code.

giorashc
  • 13,691
  • 3
  • 35
  • 71
0

You are looking for the Singleton pattern. Read the wikipedia article and you will find examples of how it can be implemented in Java.

Perhaps you'd also care to learn more about Design Patterns, then I'd suggest you read the book "Head First Design Patterns" or the original Design Patterns book by Erich Gamma et al (the former provides Java examples, the latter doesn't)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

What you are looking for is the Singleton Pattern.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118