You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.; import java.lang.reflect.;
class Singleton implements Cloneable, Serializable{ private static Singleton INSTANCE; private static boolean isInstanceCreated=false; public String str; private Singleton() {
}
public static Singleton getSingleInstance(){ if (INSTANCE == null) { synchronized (Singleton.class) { if (INSTANCE == null) { try { INSTANCE = new Singleton(); } catch (Exception e) { e.printStackTrace(); } } } } return INSTANCE; } @Override protected Object clone() throws CloneNotSupportedException { return INSTANCE; } private Object readResolve() throws ObjectStreamException { return INSTANCE; }
here is my solution, keep in mind that it is not reflection safe we can still make more then single object by using reflection api.
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 →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.; import java.lang.reflect.;
class Singleton implements Cloneable, Serializable{ private static Singleton INSTANCE; private static boolean isInstanceCreated=false; public String str; private Singleton() {
}
}
here is my solution, keep in mind that it is not reflection safe we can still make more then single object by using reflection api.