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.
- Prepare
- Java
- Advanced
- Java Singleton Pattern
- Discussions
Java Singleton Pattern
Java Singleton Pattern
Sort by
recency
|
158 Discussions
|
Please Login in order to post a comment
class Singleton{
public String str;
private static Singleton singleton;
}
Why need public instance variable "str"? I don't understand this challenge.
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.
class Singleton{ String str; private Singleton(){ } public static Singleton getSingleInstance(){ return new Singleton(); }