Why is it mandatory to have private Constructor inside a Singleton class

前端 未结 10 1135
心在旅途
心在旅途 2020-12-10 08:12

This is my singleton class for obtaining the database connection.

I have a question here: why it compulsory to have a private constructor inside a singleton class (a

10条回答
  •  无人及你
    2020-12-10 08:37

    It is mandatory. Your code may be fine, but others can use this code and instantiate another object, or you might do it by accident. This way is safe.

提交回复
热议问题