Constructors

Methods for instance creation (instantiation)

Creating An Object

Object Oriented Programming is all about creating objects (instances of a class) and communicating with those objects (calling methods). Objects contain instance variables that contain the state of an object, describing the object's attributes.

Let's say that we have a simple Parrot class that looks like this:

public class Parrot
{
   public string Name { get; set; }
}

So, far when we have created an object and defined its state, we've done it something like this:

Parrot myParrot = new Parrot();
myParrot.Name = "Dewd";

Here, we've created an instance of the Parrot class named myParrot.

myParrot has an instance variable named Name with the value "Dewd".


A Flock of Parrots

San Francisco is just the right climate for parrots from Central/South America. Flocks of parrots that have escaped from owners or been let loose flock together on Telegraph Hill. Birds of a feather do truly flock together.

I recommend tracking down a flock sometime. They're wild (err… feral).


Creating lots of objects

Let's say we need to create a lot of parrots. There's a flock. That wouldn't be too difficult, given that our parrots only have a name right now, but if we started adding other attributes, this would become more and more difficult. We're developers, not typists. Also, it would be easy to forget to assign one or more attributes. So we can make it easier and less error prone by creating a method to create Parrot instances.


Here's what we were doing before:

Parrot myParrot = new Parrot();
myParrot.Name = "Dewd";

Here's how we could create a method to do the same thing:

public static Parrot CreateParrot(string name)
{
  Parrot parrot = new Parrot();
  parrot.Name = name;
  return parrot;
}

And here's how we would call it:

Parrot myParrot = CreateParrot("Dewd");

But that still seems like a lot of work?

There's no guarantee that someone won't just skip around our factory method and create an instance on their own! Luckily, there's a cleaner way that guarantees our parrot will have a name. Remember that constructor we've been calling?

Parrot p = new Parrot();

We call this a constructor because it constructs an instance of the class.


But there's no constructor in my class!

This constructor is what is known as a default constructor. If we haven't explicitly defined a constructor for our class, the compiler creates a constructor for us that doesn't accept any arguments and does basic instance construction. That's why we don't see the constructor in our class.

The constructor that the compiler nicely creates for us if we have not explicitly declared one looks like this:

public Parrot()
{
}

Note that the constructor has neither a return type nor a return statement.

Also, the constructor name is the same as the name of its class.


We have the technology

When we want to initialize attributes of an object, we create our own constructor that accepts parameters, just like methods that accept parameters.

Remember our CreateParrot method?

public static Parrot CreateParrot(string name)
{
  Parrot parrot = new Parrot();
  parrot.Name = name;
  return parrot;
}

We can (and should) move this work into the constructor for the class. Here is our Parrot class with an equivalent constructor:

public class Parrot
{
  string Name {get;set;}

  public Parrot(string name)
  {
    Name = name; // Name is the Property.  name is the argument.
  }
}

We can construct it

Once we've created our constructor, instead of this:

Parrot myParrot = CreateParrot("Dewd");

we can do this:

Parrot myParrot = new Parrot("Dewd");

This is cleaner, more expressive, and moves the responsibility of construction to the Parrot class, where it belongs.


Happy Dance

Now that you know about constructors, its time to celebrate!

party parrot