Constructor (object-oriented programming)

In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of function called to create an object.

It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers.

A properly written constructor leaves the resulting object in a valid state.

These constructors are usually invoked implicitly to convert arguments or operands to an appropriate type, but they may also be called explicitly.

In Java, C#, and VB .NET, the constructor creates reference type objects in a special memory structure called the "heap".

The initializer list is not required, but offers the opportunity to provide values for data members and avoid separate assignment statements.

When memory allocation is required, the new and delete operators are called implicitly.

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.

Static constructors are thread safe and implement a singleton pattern.

Since ColdFusion 10,[9] CFML has also supported specifying the name of the constructor method: In Eiffel, the routines which initialize new objects are called creation procedures.

The keyword create introduces a list of procedures which can be used to initialize instances.

[11] Java provides access to the superclass's constructor through the super keyword.

The only syntactic difference to regular methods is the keyword constructor in front of the name (instead of procedure or function).

An anonymous hidden method called initializer allows to evaluate an expression immediately after the object has been built.

[13] 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.

[1] In PHP, a class is only allowed to declare a maximum of one constructor method.

Static methods, factory classes or optional constructor arguments are some ways to facilitate multiple ways to create objects of a PHP class.

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.

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.

A BUILDARGS method can be specified to handle constructor arguments not in hashref / key => value form.

In both cases the Person class is instiated like this: In Python, constructors are defined by one or both of __new__ and __init__ methods.

However the __new__ method is permitted to return something other than an instance of the class for specialised purposes.

[15] 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.