Constructor (computer science)

From Wikipedia, the free encyclopedia

In object-oriented programming, a constructor (sometimes shorted to ctor) in a class is a special block of statements called when an object is declared (statically constructed on the stack, possible in C++ but not in Java and other OO languages) or dynamically constructed on the heap through the keyword “new”. A constructor is similar to class methods, but it is not a special kind of method, because it never has an explicit return type, it's not inherited, and usually has different rules for modifiers. Constructors are simply special block of statements that are called at the moment of creation of an object (instance of a class). They are often distinguished by having the same name as the declaring class. Their responsibility is to pre-define the object's data members and to establish the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a 'valid' state.

Contents

[edit] Java

Some of the differences between constructors and other Java methods:

  • Constructors never have an explicit return type.
  • Constructors cannot be directly invoked (the keyword “new” must be used).
  • Constructors cannot be overridden, nor they are inherited.
  • Constructors cannot be synchronized.
  • Constructors are always executed by the same thread.
  • Constructors cannot be final.
  • Constructors cannot be abstract.
  • Constructors cannot be static.

[edit] Example

public class Example 
{
  //definition of the constructor. 
  public Example()
  {
     data = 1;
  }

  //overloading a constructor
  public Example(int input)
  {
     data = input;
  }

  //declaration of instance variable(s).
  protected int data;
}

[edit] REALbasic

[edit] Example

Constructors in REALbasic can be in one of two forms. Each form uses a regular method declaration with a special name (and no return value). The older form uses the same name as the Class itself, and the newer form uses the name "Constructor." The newer form is the preferred one because it makes refactoring your class easier.

Class Foobar
  // Old form
  Sub Foobar( someParam as String )
  End Sub

  // New form
  Sub Constructor()
  End Sub
End Class

[edit] C#

[edit] Example

class myClass
{
  private int mA;
  private string mB;

  public myClass(int a, string b)
  {
    mA = a;
    mB = b;
  }
}

[edit] PHP

[edit] Example

In PHP the constructor is a method named __construct(), which is automatically called by the keyword new after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor the function parameters in between the parentheses.

class Person
{
   private $name;

   function __construct($name)
   {
       $this->name = $name;
   }
 
   function getName()
   {
       return $this->name;
   }
 
}


[edit] Constructors simplified (with pseudocode)

Constructors are always part of the implementation of classes. A class (in programming) refers to a specfication of the general characteristics of the set of objects that are members of the class rather than the specific characteristics of any object at all. A simple analogy follows. Consider the set (or class, using its generic meaning) of students at some school. Thus we have

class Student {
        // refers to the class of students
        // ... more omitted ...
}

However, the class Student is just a generic prototype of what a student should be. To use it, the programmer creates each student as an object or instance of the class. This object is a real quantity of data in memory whose size, layout, characteristics, and (to some extent) behavior are determined by the class definition. The usual way of creating objects is to call a constructor (classes may in general have many independent constructors). For example,

class Student {
        Student (String studentName, String sex, String Address, int ID) {
                // ... storage of input data and other internal fields here ...
        }
        // ...