How do I restrict object creation not more than 3 in Java class?
Can you give me an idea of how I can to do it?
How do I restrict object creation not more than 3 in Java class?
Can you give me an idea of how I can to do it?
Instanciate your class via a factory (see design patterns) and limit it to 3 instances using a simple counter.
You need Modified Singleton. See this - same http://stackoverflow.com/questions/2323814/oops-design-patterns
Factory pattern is the way to go. Or a static counter can be used. Need to careful about thread safety.
You can also have a look at ObjectPool of Apache Commons. The source code is freely available...