Singleton Pattern Example

This is a thread safe, lazy load singleton example:

public class Singleton {
    private Singleton() { // make it private to prevent instantiation from outside
        // some code for the constructor

    private static class InsHolder { // Once getInstance() being called, this class will be loaded (Lazy)
        private static final Singleton INSTANCE = new Singleton();

    public static Singleton getInstance() {
        return InsHolder.INSTANCE;

The down side of the above code is you can’t throw exceptions on constructor.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s