Is there a singleton design which can completely prevent the creation of more than one object?
Asking this question keeping in mind that, in Java, we can recreate 'singleton objects' using serialization-deserialization and reflection.
Is there a singleton design which can completely prevent the creation of more than one object?
Asking this question keeping in mind that, in Java, we can recreate 'singleton objects' using serialization-deserialization and reflection.
Yes, we can design a truly singleton class (which is thread-safe too).
But each singleton class is only singleton within a given class loader,
not within the whole JVM at runtime.
For further details, check these related questions.
What is an efficient way to implement a singleton pattern in Java?
How does Singleton behave when two threads call the "getInstance()" at the same time?
How to correctly make a thread safe Singleton Factory in Java?
Yes, Java 5 introduced enums, which are resistant to having additional instances created via serialisation, etc.
The standard way to create a singleton in Java 5+ is to use a one-constant enum.
Probably yes or no, depending on what steps you go to and what cases you have in mind.
Purely within the JVM, I would say no. Resources loaded by different classloaders are considered distinct. So no matter what checks you put in place, your single Foo.class
file could be loaded by different classloaders, and two instances of your static object would be created. The static variables they referred to would be different, so one of them couldn't see that the other one was already created.
However, if you're willing to use external resources (e.g. a file on the local filesystem, assuming you can definitely write to a directory) then it might be possible. This is assuming that the external system has compare-and-set or other synchronization semantics, otherwise you have a potential race condition that might lead to both potential instances seeing that the other isn't running, and then starting up.
The latter case might be relevant depending on why you are so determined that a second instance can't exist. If it's because it might corrupt some external resource, put the constraint on that resource itself.
you can use the enum to make the singleton pattern which is the best to implement the singleton in java but keep one mind that it is one singleton per classloader
Serializable singleton
public enum Elvis {
INSTANCE;
private final String[] favoriteSongs =
{ "ABC", "DEF" };
public void printFavorites() {
System.out.println(Arrays.toString(favoriteSongs));
}
}
A single-element enum type is the best way to implement a singleton.
It will provide the serialization machinery for free, and provide an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.
Yes, you can make a truly singleton class in java.
For Example:
public class Foo {
private Foo f = null;
//More Variables
private Foo(){ //Constructor
}
public static Foo getInstance() {
if (f == null) { //f is the instance of the class Foo.
f = new Foo();
}
return f;
}
public void display() {
System.out.println("Display");
}
//...More Methods
}
In order to call, you may use it as:
Foo.getInstance().display();
And everytime, you call getInstance(), it will only make one instance of the class Foo once during the lifetime of the application.