Why to mark constructor as private in Java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why to mark constructor as private in Java ?

I know the constructor in Java can be marked as private, but I don't know why to do that ? What is the purpose of private constructor ? Please provide an example.

29th Aug 2017, 12:36 PM
Corey
Corey - avatar
1 Answer
+ 9
Example where you could use a private constructor: Singleton pattern. https://www.sololearn.com/discuss/129670/?ref=app https://www.sololearn.com/discuss/380564/?ref=app and many more threads.
29th Aug 2017, 12:39 PM
Tashi N
Tashi N - avatar