Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between singleton class and static class? [duplicate]

Possible Duplicates:
Difference between static class and singleton pattern?
What is the difference between a Singleton pattern and a static class in Java?

HI I am not clearly getting What’s the difference between a singleton class and a static class? Can anybody elaborate this with example?

like image 315
giri Avatar asked Sep 15 '10 05:09

giri


People also ask

What is difference between static class and singleton?

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. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state.

What if I use static instead making singleton?

While a static class is generally initialized when it is first loaded and it will lead to potential class loader issues. Singleton Objects stored on heap while static class stored in stack. Singleton Objects can have constructor while Static Class cannot.

Can we use static class instead of singleton in Java?

Singleton implementation can either have static members or instance members. Static classes can contain static members only. It can implement any other interface or base class is required. It cannot implement the interface or any other base class.

Can singleton class have multiple instances?

Well-designed singleton can have only one instance per application. Creating of multiple instances is a mistake in the application design. It might happen in some cases, e.g.: Non thread safe singleton with lazy initialization: several threads are trying to get an instance and creates multiple instances.


1 Answers

Singleton Class: Singleton Class is class of which only single instance can exists per classloader.

Static/Helper Class (Class with only static fields/methods): No instance of this class exists. Only fields and methods can be directly accessed as constants or helper methods.

Following is referenced from this blog "Static classes in Java" describes it nicely. The blog also has examples for explaining same:

Singleton example:

public class ClassicSingleton {     private static ClassicSingleton instance = null;      private ClassicSingleton() {         // Exists only to defeat instantiation.     }      public static ClassicSingleton getInstance() {         if (instance == null) {             instance = new ClassicSingleton();         }         return instance;     } } 

Static Class example:

/**  * A helper class with useful static utility functions.  */ public final class ActionHelper {      /**      * private constructor to stop people instantiating it.      */     private ActionHelper() {         // this is never run     }      /**      * prints hello world and then the users name      *       * @param users      *            name      */     public static void printHelloWorld(final String name) {         System.out.println("Hello World its " + name);     }  } 

So what's the difference between the two examples and why do I think the second solution is better for a class you don't want or need to instantiate. Firstly the Singleton pattern is very useful if you want to create one instance of a class. For my helper class we don't really want to instantiate any copy's of the class. The reason why you shouldn't use a Singleton class is because for this helper class we don't use any variables. The singleton class would be useful if it contained a set of variables that we wanted only one set of and the methods used those variables but in our helper class we don't use any variables apart from the ones passed in (which we make final). For this reason I don't believe we want a singleton Instance because we do not want any variables and we don't want anyone instantianting this class. So if you don't want anyone instantiating the class, which is normally if you have some kind of helper/utils class then I use the what I call the static class, a class with a private constructor and only consists of Static methods without any any variables.

Same answer is also referenced from my answer here

like image 59
YoK Avatar answered Sep 20 '22 03:09

YoK