Re: Types of constructors

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I private constructor is a constructor that is only available to the same class. They can be useful in factory scenarios, where a class creates other instances of its own class and you don't want other classes to be able to instantiate the class in the same way.

For example:

public class Foo {
  string name;

  // this is the only constructor available to other classes
  // other classes cannot set the name field directly
  public Foo(){
    this.name = "DefaultName";
  }

  // this constructor is not available to other classes
  // it allows the name field to be set
  private Foo(string name){
    this.name = name;
  }

  // other classes can call this factory method
  public Foo CreateFoo(string fooName){
    // calls the private constructor
    // and returns a new Foo with the name "fooName"
    return new Foo(fooName);
  }

  public string Name { get { return this.name; } }
}


You can also have static constructors, which can be used to initialize static fields of the class.
static Foo() { // initialize static fields }



Joshua Flanagan http://flimflan.com/blog

Sathyaish wrote:
What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.

.



Relevant Pages

  • Re: Why can I not use: Math a=new Math();
    ... > on just not declaring a constructor. ... > declaring a public constructor that throws an exception. ... cannot be instantiated (by using private constructor). ... Namespace would just be ...
    (comp.lang.java.programmer)
  • Re: Is there a way to disallow subclassing
    ... It can be done by using virtual inheritance: ... // class with private constructor and destructor ... class NotToBeSubclassed: virtual Locker ... a most-derived class must call the constructor ...
    (comp.lang.cpp)
  • Re: Why can I not use: Math a=new Math();
    ... > use test coverage tools and they report that default constructor as not ... > test to check if it has the default constructor when there is no purpose to ... If I switch to a private constructor the ... Tools that document interfaces will not ...
    (comp.lang.java.programmer)
  • Re: Types of constructors
    ... A private constructor is a constructor for a class that no one outside of ... the class (and it's descendents) can call. ... In simply factory method pattern, you may place the factory method in the ... > What is a private constructor, and why would a class have one? ...
    (comp.object)
  • Re: C++ design question
    ... so anyone can instantiate it without ... > default constructor in the class header rather that allowing it to be ... > separately and initialize a pointer to it in Foo: ... fooDeriveN::DoStuffWithBarBase() at all because we have a Bar* in Foo*. ...
    (comp.object)