views:

238

answers:

5

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?

+1  A: 

It's the difference between a pattern and how the pattern is implemented.

The Singleton pattern is not tied specifically to the Java language. There may be different ways of making a class into a singleton, depending on the language you use. Many OO languages use the equivalent of static variables to make a singleton, but others might use different techniques.

Also, some ways of implementing a singleton are better than others. One good way of implementing a Singleton is to properly encapsulate access to that Singleton through a factory method:

public class Example {
    private static final Example SINGLETON = new Example();

    public Example getInstance() { return SINGLETON; }

    private Example() { /* Constructor */ }
}

Using this technique, you can do all sorts of sophisticated things: lazy-load the singleton, replace it with some subclass, manage the singletons initialation via configuration, and so forth.

Simer
A: 

A Singleton is not a type of a class but a design pattern. With Singleton you (try to) guarantee, that only one instance of a certain class is ever constructed inside a single Java Virtual Machine. Modern implementations of the singleton pattern use enums, by the way. Older implementations use a private constructor and store the reference to the single instance in a static field.

A static class is always a member class which, in contrast to an inner class, has no access to instance variables of the surrounding class.


Static class example

public class A {
  public static class B {        
  }     
  public        int notAccessibleForB;
  public static int    accessibleForB;
}

Singleton pattern (simple old style)

public final class Singleton {
  public final static Singleton INSTANCE = new Singleton();
  private Singleton(){}
}

Singleton pattern (simple modern style)

public enum Singleton {
   INSTANCE;
}
Andreas_D
+3  A: 

Old que/ans on SO : Difference between static class and singleton pattern?

A static class is one that has only static methods, for which a better word would be "functions". The design style embodied in a static class is purely procedural.

Singleton, on the other hand, is a pattern specific to OO design. It is an instance of an object (with all the possibilities inherent in that, such as polymorphism), with a creation procedure that ensures that there is only ever one instance of that particular role over its entire lifetime.

Pranay Rana
+1  A: 

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.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:

/**
 * 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

YoK
A: 

The difference is not the correct way to ask.because singleton is not a keyword compared to static. you should be asking like "When to choose which one?". what are the advantages of singleton class over static class, these questions comes at the design stages.

Singleton: Usage: classes that serve as global configuration , ex: Trial version of software with one database connection, JDK Runtime classes instances per jvm.

When to go: 1.While developing your code,you think of forward compatibilty, like tomorrow when you need to convert this singleton class to normal class or allow subclassing. 2. You can provide lazy loading feature , when this singleton class is heavy.

static: Usage: classes that basically does conversions,utility functions. please check Math class.

When to go: 1. helper classes, used by all the classes in your api development.

disadvantage: 1. classes are eagerly loaded .

expecting points from other people.

Suresh S