1

Case 1:

public class Singleton {

  public static final Singleton INSTANCE = new Singleton();

  private Singleton() {
    ...
  }
}

Case 2:

public class Singleton {

  private static final Singleton INSTANCE = new Singleton();

  private Singleton() {
    ...
  }

  public static Singleton getInstance() {
    return INSTANCE;
  }
}

Is the second method the recommended way to implement the Singleton design pattern, because I have never seen the first one in any example for Singleton pattern.

alpha_ulrich
  • 536
  • 5
  • 21

6 Answers6

3

Without going into all the stuff about singletons being an antipattern (but you should read up on it!), the currently best way to make a singleton in Java is to use an enum.

public enum Singleton {
    INSTANCE;
}

The reason this is better is because the JVM guarantees that only one instance of the enum (per classloader) will exist at any time. It is thread safe, and you cannot use reflection to create another instance of the singleton.

Enum-values are also lazily instantiated, so you won't create the singleton before you access Singleton.INSTANCE the first time.

marstran
  • 26,413
  • 5
  • 61
  • 67
1

The best way to make a singleton is to use Enum.

public enum Foo {
    INSTANCE;
}

What is an efficient way to implement a singleton pattern in Java?

Community
  • 1
  • 1
Umair Zahid
  • 135
  • 9
0

In your example, case 1 is more simple, so it's better. But just wonder what happen if your constructor function is more complicated with some parameters. At this time, you really need some thing like this:

public class Singleton {

 private static final Singleton INSTANCE;

 private Singleton(string a, string b, string c) {
   ...
 }

public static Singleton getInstance(string a, string b, string c) {
   if (INSTANCE != null) { //note in multiple thread env, need add synchronize here.
       ......
   }
   return INSTANCE;
}
}
Luong Dinh
  • 569
  • 4
  • 17
0

The first process of creating Singleton object created the instance of Singleton class even before it is being used and that is not the best practice to use.

Also the same problem exists in second implementation. My preferred way of singleton instance creation:

 private static final Singleton INSTANCE = null;
 public static Singleton getInstance(){
      if(INSTANCE == null)
        INSTANCE = new Singleton();
    return INSTANCE;
 }

Above implementation of Singleton instance creation is okay in single threaded environment but in case of multi-threaded environment two threads may access if block at same time so they will have different instances. This could be solved as below:

private static final Singleton INSTANCE = null;
 public static Singleton getInstance(){
    if(INSTANCE == null){
        synchronized (Singleton.class) {
            if(INSTANCE == null){
                INSTANCE = new Singleton();
            }
        }
    }
    return INSTANCE;
 }

Hope this helps.

SachinSarawgi
  • 2,632
  • 20
  • 28
0

This how I implement singleton with enum

import static collections.concurrancy.ConcurrentHashMapInstanceEnum.STAFF;
public enum ConcurrentHashMapInstanceEnum {
    STAFF;
    private ConcurrentHashMap<Integer,Person> concurrentHashMap = null;

    private ConcurrentHashMapInstanceEnum() {
            concurrentHashMap = new ConcurrentHashMap(10, 5f, 4);
    }

    public ConcurrentHashMap getConcurrentHashMap() {
        return concurrentHashMap;
    }
}    

and this is how to reach in a thread...

 staffList = STAFF.getConcurrentHashMap()
Javaci
  • 61
  • 1
  • 3
-1

Yes Obviously the second method is preferred way to implement Sigleton pattern , Inside getInstance() you have to check if instance is already created then return the same and Only create new instance if there is No instance of the class.

Also Make getInstance method as static method.

Abhilash Arjaria
  • 142
  • 2
  • 10
  • You would also have to use the Method way of getting it when object instantantiation would become more complex. Your example is simply because it calls the default constructor. Think of a scenario where your singleton object would have to be obtain by a factory pattern. – Dirk Schumacher Nov 29 '16 at 10:14
  • Yes So That's why I suggest to prefer the way of use method getInstanse instead of constructor way. when you use factory pattern getInstanse will work. – Abhilash Arjaria Nov 29 '16 at 10:53