I’d like to write singleton class for java.
I found a sample program, as follows.
public class Singleton {
private static Singleton singleton = new Singleton();
private Singleton() {
System.out.println("instance is created.");
}
public static Singleton getInstance() {
return singleton;
}
}
You know, constructor is private method, not called.
How can I make a singleton instance?
When an instance is created in java program?
thank you.
The singleton instance will be created the first time you’ll call Singleton.getInstance(). The class will be loaded, and the singleton variable initialized, and returned by the getInstance() method.