I want to know that, how private constructor is useful in java.
which are the different ways to use private constructor in java.
解决方案
private constructor is off course to restrict instantiation of the class.
Actually a good use of private constructor is in Singleton Pattern. here's an example
public class ClassicSingleton {
private static ClassicSingleton instance = null;
private ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
this way you can ensure that only one instance of class is active.
Other uses can be to create a utility class, that only contains static methods.
For, more analysis you can look into other Stack overflow answers