java singleton implement interface code example
Example 1: java singleton implementation
public class SingletonClass {
private static final SingletonClass SINGLE_INSTANCE = new SingletonClass();
private SingletonClass() {}
public static SingletonClass getInstance() {
return SINGLE_INSTANCE;
}
}
Example 2: threadsafe singleton pattern in java
package com.journaldev.designpatterns;
public class ASingleton {
private static volatile ASingleton instance;
private static Object mutex = new Object();
private ASingleton() {
}
public static ASingleton getInstance() {
ASingleton result = instance;
if (result == null) {
synchronized (mutex) {
result = instance;
if (result == null)
instance = result = new ASingleton();
}
}
return result;
}
}