by BehindJava

Does getInstance() denote a singleton according the singleton pattern in Java

Home » java » Does getInstance() denote a singleton according the singleton pattern in Java

In this tutorial we are going to learn about does getInstance() denote a singleton according the singleton pattern in Java.

Calendar is not a singleton, and each call to Calendar.getInstance(…) returns a different instance. The Javadoc doesn’t say that each call will return the same instance, so you have no reason to assume that it will.

Calendar.getInstance(…) better fits the factory design pattern.

Looking at other examples of getInstance, such as Locale.getInstance(), you see that the Javadoc tells you if consecutive calls may return the same instance :

/**
 * Returns a <code>Locale</code> constructed from the given
 * <code>language</code>, <code>country</code> and
 * <code>variant</code>. If the same <code>Locale</code> instance
 * is available in the cache, then that instance is
 * returned. Otherwise, a new <code>Locale</code> instance is
 * created and cached.
 *
 * @param language lowercase two-letter ISO-639 code.
 * @param country uppercase two-letter ISO-3166 code.
 * @param variant vendor and browser specific code. See class description.
 * @return the <code>Locale</code> instance requested
 * @exception NullPointerException if any argument is null.
 */
static Locale getInstance(String language, String country, String variant)

Again, this is not a singleton, but the instances are cached, as the Javadoc says. Unless the Javadoc says so, you can expect each call to getInstance to return a different instance.

Nope, if you look to the source code, you always get a new Calendar, the method getInstace() just determines which type of Calendar you will get:

public static Calendar More ...getInstance(TimeZone zone, Locale aLocale)
{
    return createCalendar(zone, aLocale);
}

And createCalendar:

private static Calendar More ...createCalendar(TimeZone zone, Locale aLocale)
{
    // If the specified locale is a Thai locale, returns a BuddhistCalendar instance.
    if ("th".equals(aLocale.getLanguage())
        && ("TH".equals(aLocale.getCountry()))) {
        return new sun.util.BuddhistCalendar(zone, aLocale);  // new Budhist
    } else if ("JP".equals(aLocale.getVariant())
               && "JP".equals(aLocale.getCountry())
               && "ja".equals(aLocale.getLanguage())) {
        return new JapaneseImperialCalendar(zone, aLocale); // new Japanese
    }

    // else create the default calendar
    return new GregorianCalendar(zone, aLocale);   // new Gregorian
}

How do I determine a singletton in java?

Singletton returns always same instance of an object, there are different ways, but usually you will have

  • Class attribute (the object to get the instance)
  • Private constructor to avoid new creations
  • Public getInstance method that check if the attribute is null to create it, and always same instance of the object…

Something like:

public class Singleton {
    // instance attribute
    private static Singleton INSTANCE = null;

    // private constructor
    private Singleton() {}

    // public get that checks instance and return it
    public static Singleton getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }
}