[Solved] Create Singleton class and restrict to two objects in java?(Like double )


First of all, if you are gonna retain more than one instance of the class then you are actually not using Singleton pattern (as pointed out by ANS).

Anyway your question sounds like you need multiple yet limited number of instances of a class. In that case, Object Pool pattern can be useful for you. Please find the wiki link below for your reference (sample java code snippet also found in the same wiki page):

https://en.wikipedia.org/wiki/Object_pool_pattern

solved Create Singleton class and restrict to two objects in java?(Like double )