I have a singleton like this.
public class BookingFactory {
private final static BookingFactory instance;
static {
instance = new BookingFactor
Try using an enum
. enums make for good Singletons.
public static enum BookingFactory {
INSTANCE;
public static BookingFactory getInstance() {
return INSTANCE;
}
}
You can't create an enum via reflection.
The getInstance() method is superfluous but makes it easier to run your test, throwing the following exception:
java.lang.IllegalArgumentException: Cannot reflectively create enum objects
at java.lang.reflect.Constructor.newInstance(Constructor.java:530)
at MultiSingletonTest.main(MultiSingletonTest.java:40)
Oh look, someone already gave the enum answer. Posting anyway for more completeness.