2

I know this question is weird but just wondering: is there any way to create multiple instances of a Singleton class in Java?

My situation is like this:

I have an Singleton class and i need to have 2 objects/instances of that class. Is there any way to modify class to be able to create multiple instances?

My class:

public class SingletonClass {

    private static SingletonClass sSoleInstance;

    //private constructor.
    private SingletonClass(){

        //Prevent form the reflection api.
        if (sSoleInstance != null){
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    } 

    public static SingletonClass getInstance(){
        if (sSoleInstance == null){ //if there is no instance available... create new one
            sSoleInstance = new SingletonClass();
        }

        return sSoleInstance;
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Mana Illu
  • 29
  • 2
  • 4
    If you *want* to create two instances, it's not a singleton. If you *do* create two instances, it's a badly-implemented singleton (which this code is). – Andy Turner Mar 28 '17 at 19:18
  • Also, call this code from multiple threads - chances are on some invocations you'll end up with two objects anyway. See http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java. Regardless, can you expand on *why* you need two instances of this so called singleton? – James Fry Mar 28 '17 at 19:19
  • How would you decide which of the instances to return from the `getInstance()` method? Would you have `getInstance1()` and `getInstance2()`? Then, which would you call? – Andy Turner Mar 28 '17 at 19:20
  • If you have control over the class and need two instances can't you just refactor it into not being a singleton? – chatton Mar 28 '17 at 19:25
  • A singleton is _by definition_ something that you can't make two instances of. What you're looking for is ... well, any class that's _not_ a singleton. – Dawood ibn Kareem Mar 28 '17 at 19:57

2 Answers2

2

One can use the enum pattern to create singletons; like

public enum Whatever { 
  INSTANCE;
}

Turning that into a Bi-Singleton goes like:

public enum Whatever { 
  INSTANCE, YETANOTHER
}

For the record: I just made up the word "bi-singleton"; simply as this makes close to 0 sense from a conceptual point of view. If you need more than one instance, it is not a singleton; period. So your question sounds more like an XY problem.

And just a note: consider using that enum solution; as it is thread safe by default; the code you are using is not. But before making changes, do a bit of research to understand the pros and cons of those approaches.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Absolutely valid question with a valid use case - in a nutshell you can have multiple instances of a class with a private constructor when employing static factory methods. You ensure that your class cannot be instantiated from the outside world by making the constructor private, but at the same the class in question can instantiate itself as many times as it pleases.

Check this article for details and code samples. Hope that helps.

The Riddler
  • 107
  • 8