What is the use of private constructor in Java?

What is the use of private constructor in Java?

A private constructor is used when there is no requirement of another class to invoke that. It is used mostly in implementing singletons. A static method is used for managing the creating the instances of that class.

The following is the example of private constructor.
public class Singleton
{
   private static Singleton instance;.
   //private constructor
   private Singleton
   {
      // some code goes here
   }
}
Explain how to make a class immutable - Java
In order to make a class immutable, the changing state of the class object must be restricted. This means restricting an assignment to a variable...
Java methods are virtual by default. Comment
Java methods are virtual by default. The virtual methods are the methods of subclasses...
How a class implements an interface in Java.
First ,the interface is to be defined. Defining an interface is placing the method signatures...
Post your comment