I have never found good answers to these simple questions about helper/utility classes:
- Why would I create a singleton (stateless) instead of using static methods?
- Why would an object instance be needed if an object has no state?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Often, singletons are used to introduce some kind of global state to an application. (More often than really necessary, to be honest, but that’s a topic for another time.)
However, there are a few corner cases where even a stateless singleton can be useful:
Example: Synchonization objects for the C#
lockor the Javasynchronizedstatement.Example: The
Toolkit.getDefaultToolkit()method in Java will return a singleton whose exact type is system dependent.Example:
DBNull.Valuein C#.