It is mostly used to create a singleton class.
Interviews23 answers
Why private constructor is used ?
23 Answers
Munib ButtPosted Apr 28, 2020, 9:50 AM EDT
Private constructor is used to preventing creating instance of class.
Manish VernvalPosted Mar 19, 2020, 10:28 AM EDT
Main use of creating Private constructor is that we cant create the object of that class which contains Private Constructor and the another use is that you can not inherit that class. Private constructor is also use to implement singleton pettern where you just want create the object of the class just once throughout the execution of the application.
Saurabh VasaniPosted Jul 25, 2019, 9:24 PM EDT
Two thing to notice about private constructor :1. Class with private constructor can not be inherited 2. We can not create object of the class which has private constructorUse of Private constructor: Many times we do not want create instance of some of the classes like Helper, utility and common routine classes.
Amit PrakashPosted Jan 1, 2019, 8:56 PM EST
private constructor restricts developers from creating instance outside of that class. Thus can be used to implement "Singleton" design pattern.
Siddhesh ChalkePosted Oct 29, 2018, 9:09 AM EDT
private constructor is used for limit instantiation...or stop the instantiation..
Jitendra SharmaPosted Aug 20, 2018, 10:51 AM EDT
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. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.
Posted Aug 2, 2018, 7:51 AM EDT
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. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.
Posted Aug 2, 2018, 7:51 AM EDT
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. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.
Posted Aug 2, 2018, 7:51 AM EDT
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. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.
Posted Aug 2, 2018, 7:49 AM EDT
the main advantage of private constructor is to prevent the instantiation of class and also we use private constructor when we implement singleton pattern and also when we have helper classes or utility class in those we cases we don't to create instance of class rather we want to call the class by name
Chandan PrasadPosted Jul 27, 2018, 9:16 AM EDT
Private constructor:- 1.it is use in singleton design pattern in which create single object that is in it. 2.Class with private constructor cannot be inherited.3.also cannot create instance in another class.
shalini sharmaPosted Jul 24, 2018, 3:26 AM EDT
1. Private Constructor: Private Constructors are used to restrict the instantiation of object using 'new' operator.
2. It is commonly used in classes that contain static members only.
3. This type of constructors is mainly used for creating singleton object.
4. If you don't want the class to be inherited we declare its constructor private.
5. We can't initialize the class outside the class or the instance of class can't be created outside if its constructor is declared private.
6. We have to take help of nested class (Inner Class) or static method to initialize a class having private constructor.
Pravin LalgePosted Jul 19, 2018, 8:28 PM EDT
1. Private Constructor: Private Constructors are used to restrict the instantiation of object using 'new' operator.
2. It is commonly used in classes that contain static members only.
3. This type of constructors is mainly used for creating singleton object.
4. If you don't want the class to be inherited we declare its constructor private.
5. We can't initialize the class outside the class or the instance of class can't be created outside if its constructor is declared private.
6. We have to take help of nested class (Inner Class) or static method to initialize a class having private constructor.
Pravin LalgePosted Jul 19, 2018, 8:28 PM EDT
1- It doesn't allow developer to create Class Constructor.
2- When you want required class should be inhered
Kashif AsifPosted Jul 6, 2018, 3:08 AM EDT
Use of private constructor:
1. To prevent creating instances of a class when there are no instance fields or methods, such as the Math class.
2. Use if all the methods in the class are static, consider making the complete class static.
Shrimant TelgavePosted Jul 4, 2018, 10:53 PM EDT
When you don't want users to create instances of your class, then create private constructor of that class. Then how to use that class? Make a static function/property/field which will return instance of that class, and return it.
Abhijit SahooPosted Jul 3, 2018, 12:16 AM EDT
Two thing to notice about private constructor :
1. Class with private constructor can not be inherited
2. We can not create object of the class which has private constructor
Use of Private constructor:
Many times we do not want create instance of some of the classes like Helper, utility and common routine classes.
Jignesh KumarPosted Jul 2, 2018, 7:55 PM EDT
The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category.
A utility class, that only contains static methods.Generally, they are used in singleton design patterns, where the code ensures that only one instance of a class can ever be created.
Sanghamitra MohantyPosted Jun 29, 2018, 2:41 AM EDT
First of all, .subscribe is not an Angular2 thing.
That's a method that comes from rxjs library which Angular is using behind the scene.
If you can imagine yourself when subscribing to a newsletter and after the subscribing, every time that there is a new newsletter, they will send it to your home ( the method inside subscribe get's called).
That's what happens when you subscribing to a source of magazines ( which they call it Observable in rxjs library)
All the AJAX calls in Angular is using this library behind the scene and in order to use any of them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.
Also, when you're doing this
Shivam ShuklaPosted Jun 27, 2018, 4:50 AM EDT
private constructor don`t allow users to create an instance of class , mainly we are using private constructor whenever we need singleton class.
Appsierra SolutionsPosted Jun 26, 2018, 1:07 AM EDT
The private constructor is generally used in classes that contain static members only and also used to prevent creating instances of a class when there are no instance fields or methods.
Deepak VermaPosted Jun 25, 2018, 11:37 PM EDT
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.
ajay gardePosted Jun 25, 2018, 9:33 AM EDT










