I am implementing a singleton class in Java to make sure no more than one instance of the class is created.
The best way to make a singleton? Use an enum.
public enum Singleton {
INSTANCE;
public void foo(){ ... }
}
// Usage:
Singleton.INSTANCE.foo();
You get lots of help from the VM not only to avoid double instantiation, but it also helps you avoid deserialization corruption.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With