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

前端 未结 10 1141
心在旅途
心在旅途 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:34

    Otherwise everyone can create an instance of your class, so it's not a singleton any more. For a singleton by definition there can exist only one instance.

提交回复
热议问题