HCL Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
I gave the same answer. But I was asked- if you have multiple objects, you can syncronise acces using mutex. Why do you need single object?
Using synchronization it is possible to have synchronization among different instances but where as singleton is to keep a single instance of any object through out of your application life cycle.
Singleton class is used to make sure that there is only one instance of singleton class in JVM.For e.g. Logger class will be getting used by almost all the classes in the application. There is no point of creating a separate instance for logger class by different classes.
- Crystal January 11, 2016