Constructor

A constructor is a function that initializes a newly allocated object.

Related concepts:  ClassObjectMethod

Closest Wikipedia entry:  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.

Authoritative Definition

Wikipedia does not necessarily offer the best fitting definition of a concept. The exact meaning we attribute to the concept of Constructor is based on principled authoritative sources such as:

Misconceptions about Constructor
18 documented Misconceptions

Check Yourself
Misconception
AllClassesHaveDefaultConstructorJava
All classes automatically get a no-argument constructor
Misconception
CannotChainAttributeToObjectInstantiationPython
Method calls, and attribute accesses in general, cannot be chained to a constructor invocation.
Misconception
CannotChainMemberToConstructorJava
Method calls or field accesses cannot be chained to a constructor invocation
Misconception
ConstructorAllocatesJava
The constructor allocates the object
Misconception
ConstructorParameterIsFieldJava
Formal constructor parameters are equivalent to instance variables
Misconception
ConstructorReturnsObjectJava
Constructors need to return objects
Misconception
ConstructorWithoutNewJava
One can write the constructor name, without new, to instantiate a class
Misconception
ImmutableRequiresFinalParametersJava
Immutable classes need final constructor/method parameters
Misconception
InitCreatesPython
__init__ must create a new object
Misconception
InitReturnsObjectPython
__init__ needs to return an object
Misconception
MustInitializeFieldInConstructorJava
Constructors must assign values to all fields
Misconception
NoEmptyConstructorJava
A constructor must do something
Misconception
NoEmptyInitPython
__init__ must do something
Misconception
NoInsideMethodCallInConstructorJava
It is foirbidden to call other methods on the same object while inside its constructor
Misconception
ObjectLabeledWithConstructorSignatureJava
In stack and heap diagrams, an object on the heap is labeled like a stack frame of a constructor call
Misconception
ObjectsMustBeNamedJava
A variable is needed to instantiate an object
Misconception
ObjectsMustBeNamedPython
A variable is needed to instantiate an object
Misconception
ThisInConstructorIsNullJava
In a constructor, this is null

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.