Types
Parameterized constructors
Constructors that can take at least one argument are termed as parameterized constructors. When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method.Default constructors
If the programmer does not supply a constructor for an instantiable class, Java compiler inserts a default constructor into your code on your behalf. This constructor is known as default constructor. You would not find it in your source code (the java file) as it would be inserted into the code during compilation and exists in .class file. The behavior of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all. In Java, a "default constructor" refer to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class or in the absence of any programmer-defined constructors (e.g. in Java, the default constructor implicitly calls the superclass's nullary constructor, then executes an empty body). All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false (boolean type), or null (reference types)...Copy constructors
Like C++, Java also supports "Copy Constructor". But, unlike C++, Java doesn't create a default copy constructor if you don't write your own. Copy constructors define the actions performed by the compiler when copying class objects. A Copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor. While copy constructors are usually abbreviated copy ctor or cctor, they have nothing to do with class constructors used in .NET using the same abbreviation.Conversion constructors
Conversion constructors provide a means for a compiler to implicitly create an object belonging to one class based on an object of a different type. These constructors are usually invoked implicitly to convert arguments or operands to an appropriate type, but they may also be called explicitly.Move constructors
In C++, move constructors take an Rvalue reference to an object of the class, and are used to implement ownership transfer of the parameter object's resources.Syntax
*__construct
. For backwards compatibility, a method with the same name as the class will be called if __construct
method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.Constructors and Destructors__construct
. Methods with the same name as the class will trigger an E_DEPRECATED level error.
* In Perl, constructors are, by convention, named "new" and have to do a fair amount of object creation.
* In Moose object system for Perl, constructors (named ''new'') are automatically created and are extended by specifying a ''BUILD'' method.
* In Visual Basic .NET, the constructor is called "New
".
* In Python, the constructor is split over two methods, "__new__
" and "__init__
". The __new__
method is responsible for allocating memory for the instance, and receives the class as an argument (conventionally called "cls
"). The __init__
method (often called "the initialiser") is passed the newly created instance as an argument (conventionally called "self
").
* Object Pascal constructors are signified by the keyword "constructor
" and can have user-defined names (but are mostly called "Create
").
* In Objective-C, the constructor method is split across two methods, "alloc
" and "init
" with the alloc
method setting aside (allocating) memory for an instance of the class, and the init
method handling the bulk of initializing the instance. A call to the method "new
" invokes both the alloc
and the init
methods, for the class instance.
Memory organization
In Java, C#, and VB .NET, the constructor creates reference type objects in a special memory structure called the " heap". Value types (such as int, double, etc.) are created in a sequential structure called the " stack". VB .NET and C# also allow the use of the ''new'' operator to create value type objects, but these value type objects are created on the stack regardless of whether the operator is used or not. In C++, objects are created on the stack when the constructor is invoked without the new operator, and created on the heap when the constructor is invoked with the new operator. Stack objects are deleted implicitly when they go out of scope, while heap objects must be deleted implicitly by a destructor or explicitly by using the ''delete'' operator.Language details
Constructors are implemented in differentC++
In C++, the name of the constructor is the name of the class. It returns nothing. It can have parameters like any member function. Constructor functions are usually declared in the public section, but can also be declared in the protected and private sections, if the user wants to restrict access to them. The constructor has two parts. First is the initializer list which follows the parameter list and before the method body. It starts with a colon and entries are comma-separated. The initializer list is not required, but offers the opportunity to provide values for data members and avoid separate assignment statements. The initializer list is required if you have ''const'' or reference type data members, or members that do not have parameterless constructor logic. Assignments occur according to the order in which data members are declared (even if the order in the initializer list is different). The second part is the body, which is a normal method body enclosed in curly brackets. C++ allows more than one constructor. The other constructors must have different parameters. Additionally constructors which contain parameters which are given default values, must adhere to the restriction that not all parameters are given a default value. This is a situation which only matters if there is a default constructor. The constructor of a base class (or base classes) can also be called by a derived class. Constructor functions are not inherited and their addresses cannot be referenced. When memory allocation is required, the ''new'' and ''delete'' operators are called implicitly. A copy constructor has a parameter of the same type passed as ''const'' reference, for example ''Vector(const Vector& rhs)''. If it is not provided explicitly, the compiler uses the copy constructor for each member variable or simply copies values in case of primitive types. The default implementation is not efficient if the class has dynamically allocated members (or handles to other resources), because it can lead to double calls to ''delete'' (or double release of resources) upon destruction.C#
Example C# constructor:C# static constructor
In C#, a ''static constructor'' is a static data initializer. Static constructors are also called ''class constructors''. Since the actual method generated has the name ''.cctor'' they are often also called "cctors". Static constructors allow complex static variable initialization. Static constructors are called implicitly when the class is first accessed. Any call to a class (static or constructor call), triggers the static constructor execution. Static constructors are thread safe and implement a singleton pattern. When used in a generic programming class, static constructors are called at every new generic instantiation one per type. Static variables are instantiated as well.ColdFusion Markup Language (CFML)
ColdFusion Markup Language (CFML) uses a method named 'init
' as a constructor method.
Cheese.cfc
Eiffel
In Eiffel, the routines which initialize new objects are called ''creation procedures''. Creation procedures have the following traits: * Creation procedures have no explicit return type (by definition of ''procedure''). * Creation procedures are named. * Creation procedures are designated by name as creation procedures in the text of the class. * Creation procedures can be explicitly invoked to re-initialize existing objects. * Every effective (i.e., concrete or non-abstract) class must designate at least one creation procedure. * Creation procedures must leave the newly initialized object in a state that satisfies the class invariant. Although object creation involves some subtleties, the creation of an attribute with a typical declarationx: T
as expressed in a creation instruction create x.make
consists of the following sequence of steps:
* Create a new direct instance of type T
.
* Execute the creation procedure make
to the newly created instance.
* Attach the newly initialized object to the entity x
.
In the first snippet below, class POINT
is defined. The procedure make
is coded after the keyword feature
.
The keyword create
introduces a list of procedures which can be used to initialize instances. In this case the list includes default_create
, a procedure with an empty implementation inherited from class ANY
, and the make
procedure coded within the class.
POINT
has a declarations my_point_1
and my_point_2
of type POINT
.
In procedural code, my_point_1
is created as the origin (0.0, 0.0). Because no creation procedure is specified, the procedure default_create
inherited from class ANY
is used. This line could have been coded create my_point_1.default_create
.
Only procedures named as creation procedures can be used in an instruction with the create
keyword.
Next is a creation instruction for my_point_2
, providing initial values for the my_point_2
's coordinates.
The third instruction makes an ordinary instance call to the make
procedure to reinitialize the instance attached to my_point_2
with different values.
F#
In F#, a constructor can include anylet
or do
statements defined in a class. let
statements define private fields and do
statements execute code. Additional constructors can be defined using the new
keyword.
Java
Innew
” invokes them).
* Constructors should not have non-access modifiers.
Java constructors perform the following tasks in the following order:
# Call the default constructor of the superclass if no constructor is defined.
# Initialize member variables to the specified values.
# Executes the body of the constructor.
Java permit users to call one constructor in another constructor using this()
keyword.
But this()
must be first statement.
super
keyword.
JavaScript
As of ES6,Object Pascal
In Object Pascal, the constructor is similar to a factory method. The only syntactic difference to regular methods is the keywordconstructor
in front of the name (instead of procedure
or function
). It can have any name, though the convention is to have Create
as prefix, such as in CreateWithFormatting
. Creating an instance of a class works like calling a static method of a class: TPerson.Create('Peter')
.
OCaml
In OCaml, there is one constructor. Parameters are defined right after the class name. They can be used to initialize instance variables and are accessible throughout the class. An anonymous hidden method calledinitializer
allows to evaluate an expression immediately after the object has been built.
PHP
In PHP version 5 and above, the constructor is a method named__construct()
(notice that it's a double underscore), which the keyword new
automatically calls after creating the object. It is usually used to automatically perform 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 arguments for the parameters.
Perl 5
In Perl version 5, by default, constructors are factory methods, that is, methods that create and return the object, concretely meaning create and return a blessed reference. A typical object is a reference to a hash, though rarely references to other types are used too. By convention the only constructor is named ''new'', though it is allowed to name it otherwise, or to have multiple constructors. For example, a Person class may have a constructor named ''new'', and a constructor ''new_from_file'' which reads a file for Person attributes, and ''new_from_person'' which uses another Person object as a template.Perl 5 with Moose
In the Moose object system for Perl, most of this boilerplate can be omitted, a default ''new'' is created, attributes can be specified, and whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which the Moose generated constructor will call, after it has checked the arguments. A ''BUILDARGS'' method can be specified to handle constructor arguments not in hashref / key => value form.Python
In Python, constructors are defined by one or both of__new__
and __init__
methods. A new instance is created by calling the class as if it were a function, which calls the __new__
and __init__
methods. If a constructor method is not defined in the class, the next one found in the class's Method Resolution Order will be called.
In the typical case, only the __init__
method need be defined. (The most common exception is for immutable objects.)
__new__
method is permitted to return something other than an instance of the class for specialised purposes. In that case, the __init__
is not invoked.
Raku
In Raku, even more boilerplate can be omitted, given that a default ''new'' method is inherited, attributes can be specified, and whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which will get called to allow for custom initialization. A ''TWEAK'' method can be specified to post-process any attributes already (implicitly) initialized.Ruby
In Ruby, constructors are created by defining a method calledinitialize
. This method is executed to initialize each new instance.
Visual Basic .NET
In Visual Basic .NET, constructors use a method declaration with the name "New
".
See also
* Resource acquisition is initialization (RAII) * Allocation site * Creational pattern * Destructor (computer programming) * Global constructor in C++, and its C counterpart, ((constructor)) function attributeNotes
References
{{Reflist, 30em Method (computer programming) Programming language comparisons