Constructor (computer science)
From Wikipedia, the free encyclopedia
In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in Java and other object-oriented languages) or dynamically constructed on the heap through the keyword “new
”.
A constructor is similar to an instance method, but it differs from a method in that it never has an explicit return type, it's not inherited, and usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class. Their responsibility is to initialize 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. Immutable objects must be initialized in a constructor.
The term constructor is also used to denote one of the tags that wraps data in an algebraic data type. This is a different usage than in this article. For more information, see algebraic data type.
In most languages, the constructor can be overloaded in that there can be more than one constructor for a class, each having different parameters. Some languages take consideration of some special types of constructors:
- default constructor - a constructor which can take no arguments
- copy constructor - a constructor which takes one argument of the type of the class (or a reference thereof)
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 synchronized, final, abstract, native, nor static.
- Constructors are always executed by the same thread.
[edit] Example
public class Example { //definition of the constructor. public Example() { this(1); } //overloading a constructor public Example(int input) { data = input; } //declaration of instance variable(s). private int data; }
//code somewhere else //instantiating an object with the above constructor Example e = new Example(42);
[edit] Visual Basic .NET
Constructors in Visual Basic use a regular method declaration with the name "New
".
[edit] Example
Class Foobar Private strData As String ' property data Public Property Data() As String Get Return strData End Get Set(ByVal strNewData As String) strData = strNewData End Set End Property ' Constructor Public Sub New(ByVal someParam As String) Data = someParam End Sub End Class
' code somewhere else ' instantiating an object with the above constructor Dim foo As New Foobar(".NET")
[edit] C#
[edit] Example
class myClass { private int mA; private string mB; public myClass(int a, string b) { mA = a; mB = b; } }
//code somewhere //instantiating an object with the constructor above myClass c = new myClass(42, "string");
[edit] ColdFusion
[edit] Example
It's important to note that there is no constructor method in ColdFusion. A common practice among developers in the ColdFusion community is to create an 'init
' method that acts as a pseudo-constructor.
<cfcomponent displayname="Cheese"> <!--- properties ---> <cfset variables.cheeseName = "" /> <!--- pseudo-constructor ---> <cffunction name="init" returntype="Cheese"> <cfargument name="cheeseName" type="string" required="true" /> <cfset variables.cheeseName = arguments.cheeseName /> <cfreturn this /> </cffunction> </cfcomponent>
[edit] PHP
[edit] Example
In PHP (Version 5 and above) 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; } }
However, constructor in PHP version 4 (and earlier) is a method in a class with the same name of the class.
class Person { private $name; function Person($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 specification 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 Address, int ID) { // ... storage of input data and other internal fields here ... } // ... }