Why do we need a private constructor?

后端 未结 10 557
一整个雨季
一整个雨季 2020-11-30 18:20

If a class has a private constructor then it can\'t be instantiated. So, if I don\'t want my class to be instantiated and still use it, then I can make it static.

Wh

10条回答
  •  一生所求
    2020-11-30 18:50

    If you want to create a factory for a class, you can use a private constructur, and add some static "factory" methods to the class itself to create the class.

    An example for this is the Graphics class, with the From* methods.

提交回复
热议问题