can you pl help me out on this problem ..i have spent a lot of time on this question but iam unable to find the answer
What if i want to write a class in such a way that only one instance of it can be created , and anyone who wants to use an instance of the class will always use that one single instance

i know i will have to use static variables and constructors some where and later once the object is created the constructor must turn private..can anyone pl help how should i overcome this problem given above

private static variable to hold the instance.
public static method getInstance() that creates an instance if necessary, and returns the instance. Should be synchronized if it's a multi-threaded app.
private constructor, so you can't call the constructor from outside.

This is called the "singleton" pattern, you'll find lots of info on the web.

private static MyClass currentInstance;

	private MyClass () {
		currentInstance = this;
	}

	public static synchronized MyClass  getInstance() {
                if (currentInstance == null) currentInstance = new MyClass();
		return currentInstance;
	}

If you can afford eager loading, a better way would be to declare your singleton instance as:

public class MySingleton {

  private static final MySingleton instance = 
    new MySingleton();

  private MySingleton() {
    // initialization code goes here
  }

  public static MySingleton getInstance() {
    return instance;
  }

  public void doSomething() {
    // do something useful
  }

}

This approach does away with all the crazy synchronization stuff and is guaranteed to work since we can be rest assured that by the time `getInstance' is called, the static field `instance' would have been initialized to a new instance of `MySingleton'.

As of Java 5, the best way to implement the Singleton Pattern is to use an `enum' [as mentioned in Effective Java 2nd Edition].

public enum MySingleton {

  SINGLETON;
  
  private MySingleton() {
    // initialization code goes here
  }

  public void doSomething() {
    // do something useful
  }

}

This provides all the serialization and synchronization for free [serialization gotchas exist when using Singletons].

Interesting solutions sos. In practice I suspect the majority of uses of singleton (in fact 100% of all the cases I can remember using) needed to be initialised the first time they were needed, not when the class was loaded. However, if that's not a problem, I really like the enum.

Hi code like this:

public class OnlyOne{
 private static OnlyOne one = new OnlyOne();
 private OnlyOne() {} // private constructor this can't be instantiated from outside.

public static OnlyOne getInstance(){
  return one;
}
}

// look at the code above,.. static variable will get initialised when the class get loaded, and then it will have reference value of object.
and when u call getInstance() you will get the same instance..

Try it.. this is practically used by me.. this is the solution.

// To use it OnlyOne myOne = OnlyOne.getInstance(); if i am helpful add to my reputation.

if i am helpful add to my reputation.

All you did is repeat part of sos's post from 2 days ago. If you want to build a reputation you need to read the earlier posts, then make a contribution by adding something new.

This article has been dead for over six months. Start a new discussion instead.