Singleton Class – Examples In Java, Kotlin, Dart, C++, C#, PHP, Python

Link Copied To Clipboard !

singleton_class_implementation CS Subjects

You might have come across a term called singleton while programming or reading some documentations. Singleton is, as its name says, single instance of a class. There are so many use cases where using singleton class is preferably the best practice.

What is Singleton Class ?

Singleton class is a class that can have only one single instance or object throughout the runtime. It means, we can not create multiple instances of the class which is singleton. After the creating of first instance, the other object creating statement simply return the already created instance rather than creating new one.

Singleton class follows singleton pattern of software engineering which restricts the creation of instances or objects to one.

The term singleton comes from mathematical concepts of singleton. In mathematics, singleton is a unit set or a set with exactly one element.

Implementation Of Singleton Class

To implement singleton class, one should consider the following constraints:-

  1. Only one instance of singleton class can ever exist
  2. There is global access to the instance

Generally, satisfying these conditions involve:-

  1. Making all constructors private
  2. Providing static method that returns the reference to the instance

The basic method is that, making instance variable private and initializing it only the first time. This can be done by checking whether the instance is null or not. If the instance variable is not null, instead of creating new instance, simply return the existing instance. Also making all constructors prohibits the new ClassName() initialization. Now, lets see some implementations on various languages.

Singleton Class In Java

public class MySingletonClass {

   private static MySingletonClass instance = null;
   private MySingletonClass() {
      // only to prevent instantiation

   public static MySingletonClass getInstance() {
      if(instance == null) {
         instance = new MySingletonClass();
      return instance;

Singleton Class In Kotlin

object MySingletonClass {
    fun foo():Int {
        return 10

    fun bar() {
        //do something

Singleton Class In Dart

class MySingletonClass {
    static MySingletonClass get instance => MySingletonClass._();

Singleton Class In C++

class MySingletonClass {
   static MySingletonClass *instance;
   // Private constructor so that no objects can be created.
   MySingletonClass() {


   static MySingletonClass *getInstance() {
      if (!instance)
      instance = new MySingletonClass;
      return instance;


Singleton Class In C#

public sealed class MySingletonClass
    public static MySingletonClass Instance { get; } = new MySingletonClass();

    private MySingletonClass() { }

Singleton Class In PHP

class MySingletonClass
    private static $instance = null;

    private function __construct() {}

    public static function getInstance(): self
        if (null === self::$instance) {
            self::$instance = new self();

        return self::$instance;

Singleton Class In Python

class MySingletonClass:
    __instance = None
    def __new__(cls, *args):
        if cls.__instance is None:
            cls.__instance = object.__new__(cls, *args)
        return cls.__instance

That’s all for now. Hope you found what you were looking for. While seeing the code and understanding the concept of singleton class pattern, you might get shocked that singleton design pattern is actually considered as Anti-Pattern .

Why singleton pattern is called anti-pattern ?

  1. Singleton class breaks OOD(Object Oriented Design) principles
  2. Singleton class does not allow Test Driven Development(TDD)

How does singleton class break OOD principles ?

  1. Can not be inherited from
  2. No control over instantiation
  3. Prevents Dependency Injection

How does singleton class not allow TDD ?

  1. Since there is no control over instance creation, a “clean” object can not be used for each test.
  2. Mock objects can not be used with dependency injection

You May Also Like