Can we have private constructor in Java?
I'll answer
Earn 20 gold coins for an accepted answer.20
Earn 20 gold coins for an accepted answer.
40more
40more
Studied at the University of Zurich, Lives in Zurich, Switzerland.
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-07 12:30:36
评论(499)
Helpful(122)
Helpful
Helpful(2)

Noah Lee
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.