10
Reply

What is the difference between Singleton and Static class

Anant Kulkarni

Anant Kulkarni

9y
4.7k
1
Reply

    Main difference between in Singleton and Static . Singleton will be available when its needed in the program. you dont need to allocate memory if its not required at the starting time of application. Static will be initialized at the beginning time it self. and it will be available at the program.

    Singleton: 1. Singleton class contains both static and non-static members 2. You can create instance of Singleton class. 3. You can implement inheritance. 4. You can clone a singleton class object. 5. Singleton class object creates in Heap memory. Static Class: 1. Static class contains only static members. 2. You can't create instance of Static class. 3. Once you declare a class as static you can't implement inheritance. 4. You can't clone static class object. 5. Static class object creates in stack memory.

    Singleton Class : We use this when we need to create only a single instance of the class.Static Class : Static Class can have static constructor but it can't have instance constructor. It calls automatically when program loads in memory.

    Singleton: single instance can be created at any point of time.Static: No instance of the class is created.

    Please have a look at below article, it may help. http://www.c-sharpcorner.com/uploadfile/19b1bd/design-patterns-simplified-part-2-singleton/

    Singleton Class: you can create one instance of the object and reuse it. Singleton instance is created for the first time when the user requested. singleton class can have constructor.Static Class: You can not create the instance of static class. are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded. Static Class class cannot have constructor.

    Singleton class can be inherited from another class. Static class cannot be inherited.

    You cannot apply new keyword to both static class and singleton classes. But CLR will load this static class, you can only load singleton class.

    http://javarevisited.blogspot.in/2013/03/difference-between-singleton-pattern-vs-static-class-java.html

    The Singleton class should be used where an instance of an object is required to be passed to some method. Since static class object cannot be created, we wont be able to pass it around as an instance.