I need a singleton in my code. I implemented it in Java and it works well. The reason I did it, is to ensure that in a mulitple environment, there is only one instance of th
You can invoke the private constructor of your singleton class using reflection to create a new instance of the class.
class MySingleton {
private MySingleton() {
}
}
class Test {
public void test() throws Exception {
Constructor constructor = MySingleton.class.getConstructor();
constructor.setAccessible(true);
MySingleton otherSingleton = constructor.newInstance();
}
}