0

Can the implementation for a Singleton class can be as simple as below :

public class MyClass {
    private final static MyClass myClass = new MyClass();
    private MyClass() {}
    public static MyClass getInstance() {
        return myClass;
    }
}

Over :

public class MyClass {
    private final static MyClass myClass;
    private MyClass() {}
    public static MyClass getInstance() {
        if(null == myClass) {
            myClass = new MyClass();
        }
        return myClass;
    }
}

Which is the better of the above implementations & why?

One thing that I observed is for the first implementation above the object constructor gets called before the static block if any.

Also if there is a thread-safe version of the 2nd implementation (may be a double null check and synchronized block), which should be preferred ?

Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
  • I would use the first option, since it is thread safe and more comprehensible. I guess the only difference is for when you want the constructor to run – EDToaster Feb 26 '15 at 05:08

3 Answers3

1

Yes. And you can use that first version, but I would suggest you use an enum. Wikipedia's entry on Singleton says

In the second edition of his book Effective Java, Joshua Bloch claims that a single-element enum type is the best way to implement a singleton for any Java that supports enums.

Something like

public enum Singleton {
    INSTANCE;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

The principle rules which yoy need to take care to define the singleton class is :

  1. Private constructors - which restricts the object creation from other classes
  2. Private static field variable which holds the only instance of the same class
  3. A public static method which is used to return this instance when requested

It depends on how you want it, whether you want early initialization or lazy initialization. The first one is an example of early initialization, while the second one is an example of lazy initialization. There are multiple ways to define a perfect Singleton Class

shikjohari
  • 2,278
  • 11
  • 23
1

I would like to put some light on singleton design pattern.

Your First Code Snippet

public class MyClass {
    private final static MyClass myClass = new MyClass();
    public static MyClass getInstance() {
        return myClass;
    }
}

The above approach works fine but has drawback as instance is getting created much before actually it is required so at run-time think of the situation that if the instance is not big you can keep it if it is unused but if it big then what is the purpose of creating the instance.This approach is called Eager initialization

The code shown below indicates the second approach called Lazy initialization public class MyClass {

    private final static MyClass myClass;
    public static MyClass getInstance() {
        if(null == myClass) {
            myClass = new MyClass();
        }
        return myClass;
    }
}

But here again there is one drawback in your code,let's understand the defect in the above code

Let's consider we have tow threads namely T1 and T2 and both threads are intended to create the instance and executes the check null == myClass,now both threads have identified instance variable to null thus assume they must create an instance. They sequentially goes to synchronized block and create the instances. At the end, we have two instances in our application.

This can be resolved by simply double check locking. Below code will show you the better way of implementation. Just note- I have used private constructor in code.

public class MyClass {
    private final static MyClass myClass = null;

//private constructor
private MyClass(){
}
    public static MyClass getInstance() {
        if(myClass == null) {
synchronized (MyClass.class){
// Double check
if(myClass == null){
            myClass = new MyClass();
        }
}

}
        return myClass;
    }
}
sorabh
  • 21
  • 5