Java private constructor - Explain the impact of private constructor.

Explain the impact of private constructor.

Private Constructors can't be access from any derived classes neither from another class. So you have to provide a public function that calls the private constructor if the object has not been initailized, or you have to return an instance to the object, if it was initialized.

This can be useful for objects that can't be instantiated.

What is private constructor? What is the impact of private constructor?

A private constructor prevents external instantiation of its containing class and sub classing. Objects can be created. But the creation is done internally.

A private constructor can be used in the following cases:

- Classes that have only static methods and members
- Classes that have only widely used static final members (constants)
- To utilize singletons
- To utilize factory methods
- To use enumerations that are type safe
Java Static Initializers - What are static Initializers?
Java Static Initializers - A static initializer block resembles a method with no name, no arguments, and no return type...
Purpose of the wait (), notify (), and notifyAll() methods
notify() Wakes up a single thread that is waiting on this object's monitor...
Advantage of event-delegation model over the earlier event inheritance model?
Event-delegation model allows a separation between a component's design.....
Post your comment