471,624 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

Types of constructors

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.

Jul 21 '05 #1
3 1365
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.

Jul 21 '05 #2
Thank you, Joshua. I understood it. That was a nice explanation.

Jul 21 '05 #3
"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.


In addition to Joshua's explanation, they are also useful to create a static
class. This is generally a sealed class with only a private constructor.
Unlike a factory class, it does not provide methods to create the class, and
only provides static methods.
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Cliff Harris | last post: by
4 posts views Thread by Sathyaish | last post: by
6 posts views Thread by Steven Livingstone | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.