Seo Forum & AI

Coding & Programming => Programming Forum => Topic started by: beingchinmay on 03-06-2017, 07:09:09

Title: What is a Private Constructor?
Post by: beingchinmay on 03-06-2017, 07:09:09
 Used to prevent the user to instantiate the class directly.
 Used to prevent the creation of instances of a class when there are no instance fields or methods
 A private constructor is a special instance constructor.
 It is commonly used in classes that contain static members only.
 If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
 Note that if you don't use an access modifier with the constructor it will still be private by default.
 Private constructors are used to restrict the instantiation of object using 'new' operator.
 This type of constructors is mainly used for creating singleton object.
 Can use nested class (Inner Class) or static method to initialize a class having private constructor.
 Example of Private Constructor - Math class
Title: Re: What is a Private Constructor?
Post by: richardmsmith on 08-31-2018, 01:18:12
It is an 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.
Title: Re: What is a Private Constructor?
Post by: greatshivam on 02-24-2020, 07:46:45
The private constructor is useful in case we want to restrict the object creation. For example, Singleton pattern can be implemented using a private constructor.

Example-

public class Tester {
   private static Tester instance;
   private Tester(){}

   public static Tester getInstance(){
      if(instance == null){
         instance = new Tester();
      }
      return instance;
   }

   public static void main(String[] args) {
      Tester tester = Tester.getInstance();
      Tester tester1 = Tester.getInstance();
      System.out.println(tester.equals(tester1));
   } 
}

It will print the output as-

true
Title: Re: What is a Private Constructor?
Post by: Lishmalinyjames on 01-27-2021, 08:56:44
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.
Title: Re: What is a Private Constructor?
Post by: saravanan28 on 04-27-2021, 02:58:29
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.
Title: Re: What is a Private Constructor?
Post by: Lishmalinyjames on 07-08-2021, 11:01:58
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.
Title: Re: What is a Private Constructor?
Post by: dark404 on 09-21-2022, 05:30:01
It is a unique occurrence constructor. For the most part utilized in classes contain static individuals as it were. In the event that a class has at least one confidential constructors and no open constructors, different classes (with the exception of settled classes) can't make cases of this class.
Title: Re: What is a Private Constructor?
Post by: codiant on 12-27-2022, 01:47:00
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.