InterviewSolution
| 1. |
What is Private Constructor, what is the use of it? |
|
Answer» A private constructor is a special instance constructor. It is generally used in CLASSES that contain static members only. If a class has one or more private constructors and no public constructors, other classes (EXCEPT nested classes) cannot create INSTANCES of this class. For example: class NLOG { // Private Constructor: private NLog() { } public static double e = Math.E; //2.71828... }The declaration of the empty constructor prevents the AUTOMATIC generation of a default constructor. Note that if you do not use an access modifier with the constructor it will still be private by default. Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. |
|