Instantiate singleton object using Class.forName()?
constructor, default, instance, java, singleton
Solution
A classic singleton also has a static getInstance() method - call that via reflection instead of using newInstance(). Yeah, it's more work, but that's how it is...
Or you could use setAccessible() to call the private constructor anyway, but you'd break the singleton and go to hell.
Thirdly, you could avoid having a Singleton altogether and find a better soltion (there usually is).
Problem
I want to instantiate one instance of a class from the string name of the class. ( using Class.forName().newInstance(). ) Here's the problem: I want that instance to be a singleton.. I could do this using a singleton pattern, except that newInstance calls the default constructor for a class, and with a singleton, that constructor must be "private".. Is there a solution? I could think of a less elegant way to do this (using a hashmap as a lookup table..), but would prefer a better solution.. Thanks,