1

I understand that Singelton helps to instantiate only one class AT A TIME. I try to learn how to Design for Singleton function in java. I want to know it better to understand Kernel. So I try to do this following, but I like to know if it's the only way to come up with private constructor.

public class Singleton {
    private static Singleton instance = null;

    private Singleton() {   }

    public static synchronized Singleton getInstance() {
        if (instance == null) {
                    instance = new Singleton ();
        }
        return instance;
    }
}
  • 2
    I'm not exactly sure what you're asking here. Your answer might be in the wikipedia page on singleton that lists 5 different ways to do it. – user40980 Apr 10 '14 at 22:11
  • Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat Apr 11 '14 at 6:33
  • Jon Skeet has written several detailed treatments of Singletons, in both C# and Java. – Robert Harvey Apr 12 '14 at 15:49
  • Any class which you want to be available to whole application and whole only one instance is viable is candidate of becoming Singleton. One example of this is Runtime class , since on whole java application only one runtime environment can be possible making Runtime Singleton is right decision. Another example is a utility classes like Popup in GUI application, if you want to show popup with message you can have one PopUp class on whole GUI application and anytime just get its instance, and call show() with message. – Ninja2000 Apr 13 '14 at 5:55
1

yes, to create a Singleton class, you have to use the private constructor as it is the only way to prevent another class from creating an instance of your class.

public class Singleton {
private static Singleton instance = null;

private Singleton() {   }

public static synchronized Singleton getInstance() {
    if (instance == null) {
                instance = new Singleton ();
    }
    return instance;
}

}

  • This answer merely repeats the code and adds almost nothing. Please improve it or delete it. – david.pfx Apr 12 '14 at 15:25
2

on of the biggest misconceptions that I see is that lazy instantiation is needed for all singletons, it isn't.

Java has it's own lazy loading of classes that will allow for lazy instantiation when the class is first needed:

public class Singleton {
    private static Singleton instance = new Singleton ();

    private Singleton() {   }

    public static Singleton getInstance() {
        return instance;
    }
}

often this is all you need for lazy instantiation of a singleton in java

0

If I understand your question correctly, then yes, to create a Singleton class, you have to use the private constructor as it is the only way to prevent another class from creating an instance of your class.

If your question is another way of getting around getInstance(), then it is also possible to use a private static inner class and either use that to store the reference as in this android related question.

public class Singleton {

    private Singleton() {}

    public static synchronized Singleton getInstance() {
        return Implementation.INSTANCE;
    }

    private static class Implementation {
        public static final Singleton INSTANCE = new Singleton();
    }
}
  • The overhead of synchronized should not be needed here as the instance is created statically and not lazy-loaded. – jhr Apr 11 '14 at 6:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.