We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
IMHO, @dvt32's solution is the best. You can avoid checking "instance == null" and meanwhile create a thread safe Singleton instance during class loading. Alternatively, you can still use lazy initialization thread safe Singleton, using synchronized. Find more from my post
class Singleton {
private Singleton() {}
public String str;
private static final Singleton instance = new Singleton();
public static Singleton getSingleInstance() {
return instance;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
IMHO, @dvt32's solution is the best. You can avoid checking "instance == null" and meanwhile create a thread safe Singleton instance during class loading. Alternatively, you can still use lazy initialization thread safe Singleton, using synchronized. Find more from my post