- Constructor (computer science)
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 member s 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 object s 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, seealgebraic 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 that can take no arguments
*copy constructor - a constructor that takes one argument of the type of the class (or a reference thereof)
= 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.Apart from this java constructor's performs the following functions in the said order.
1) Initializes the class variables to default values i.e byte,short,int,long or char variable to 0,float variable to 0.0f, double variable to 0.0, boolean variable to false and references of any objects to null.
2) It then calls the super class constructor (default constructor of super class only if no constructor is defined).
3) It then initializes the class variables to the specified values like ex: int var = 10; or float var = 10.0f and so on.
4) It then executes the body of the constructor.Example
Visual Basic .NET Constructors in Visual Basic use a method declaration with the name "
New
".Example
= C# =Example
ColdFusion 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.PHP Example
In PHP (Version 5 and above) the constructor is a method named
__construct()
, which is automatically called by the keywordnew
after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, inwhich case, when thenew
statement is written, you also need to send the constructor the function parameters in between the parentheses.However, constructor in PHP version 4 (and earlier) is a method in a class with the same name of the class.
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
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,ee also
*Destructor
Wikimedia Foundation. 2010.