Can we declare a constructor as private?
I'll answer
Earn 20 gold coins for an accepted answer.20
Earn 20 gold coins for an accepted answer.
40more
40more
Studied at Princeton University, Lives in Princeton, NJ
Some reasons where you may need private constructor: The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category. A utility class, that only contains static methods. Yes, class can have a private constructor.
2023-05-05 12:30:29
评论(499)
Helpful(122)
Helpful
Helpful(2)

Lucas Brown
QuesHub.com delivers expert answers and knowledge to you.
Some reasons where you may need private constructor: The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category. A utility class, that only contains static methods. Yes, class can have a private constructor.