How to use singleton class in JAVA.?
- Posted:
- 3+ months ago by vaibhav123
- Topics:
- computer, java
Answers (2)
The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.
Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.
For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.