When to use "private" constructor?
The private constructors are used in the following situation:
- When you implement
singletonsorfactorydesign pattern. - Do not want people to instantiate your class directly. e.g., new MyObject();. For example, all of you method and fields are static, you may need to add a private default constructor (if you do not have it, compiler will add one for you that is not what you want). Such way can enforce that people can not accidently use "new".
Most Recent java Faqs
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?
- What is runtime polymorphism in Java?
- How to add BASIC Authentication into HttpURLConnection?
- What is String literal pool?
- Can the run() method be called directly to start a thread?
- How to read input from console (keyboard) in Java?
- What does Class.forname method do?