Activity Forums Salesforce® Discussions What is difference between static and singleton class?

  • What is difference between static and singleton class?

    Posted by Marziya on December 21, 2020 at 1:02 pm

    What is difference between static and singleton class?

    Aditya replied 4 years ago 4 Members · 3 Replies
  • 3 Replies
  • Anjali

    Member
    December 21, 2020 at 2:10 pm

    A singleton allows access to a single created instance - that instance (or rather, a reference to that instance) can be passed as a parameter to other methods, and treated as a normal object. A static class allows only static methods. Singleton objects are stored in Heap, but static objects are stored in stack.

  • Shweta

    Member
    December 23, 2020 at 5:36 pm

    Static classes are basically used when you want to store a single instance, data that should be accessed globally throughout your application. The class will be initialized at any time but mostly it is initialized lazily. Lazy initialization means it is initialized at the last possible moment of time. There is a disadvantage of using static classes. You never can change how it behaves after the class is decorated with the static keyword.Singleton class does not require you to use the static keyword everywhere. Static class objects cannot be passed as parameters to other methods whereas we can pass instances of a singleton as a parameter to another method.

  • Aditya

    Member
    December 24, 2020 at 7:39 am

    A singleton allows a class for which there is just one, persistent instance across the lifetime of an application. ... While a static class allows only static methods and and you cannot pass static class as parameter. A Singleton can implement interfaces, inherit from other classes and allow inheritance.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos