Constructors

What Are Constructors in Computer Science?

In computer science, a constructor is the routine that runs when an object is created and sets its starting state. Compare C++, Java, C# and Python.

On this page
  1. The short answer
  2. Why object-oriented languages have them
  3. How common languages write them
  4. C has no constructors
  5. Constructors and destructors
  6. Where to start

The short answer

In computer science, a constructor is a special routine in an object-oriented language. It runs automatically when an object is created and sets the object’s starting state. In C++, Java and C#, a constructor is named after its class and has no return type. The language calls it for you when the object is created.

Why object-oriented languages have them

An object holds data, often several pieces that must agree with each other. If an object could exist before its data was set, every part of the program that used it would need to check whether the setup had happened. A constructor is the place where that setup happens, so a new object always starts from a known state.

How common languages write them

The idea is the same across languages, but the syntax changes:

Language Constructor form
C++ A function with the class name and no return type, such as Book(std::string title, int pages)
Java The same idea: public Book(String title, int pages)
C# The same idea: public Book(string title, int pages)
Python A method named __init__, run after the object is created
Swift A method named init
Kotlin A primary constructor in the class header, plus secondary constructors declared with the constructor keyword
C No constructors. You write a function that returns a filled-in struct

Some people also use the word initializer for the same job. Python’s __init__ is one example, since the object already exists when that method runs.

C has no constructors

C has no classes, so it has no constructors. A common C approach is a plain function that builds a struct and returns it:

typedef struct {
    int width;
    int height;
} Rectangle;

Rectangle make_rectangle(int width, int height) {
    Rectangle r = { width, height };
    return r;
}

make_rectangle is an ordinary function, not a constructor. Nothing forces the programmer to call it, and a struct can still be declared and left with unset fields. That gap is what constructors close in languages that have them.

Constructors and destructors

In C++, a destructor, written ~Book(), runs automatically when an object goes away. It is the natural counterpart to a constructor: one sets up the object, and the other cleans up after it.

Many object-oriented languages have a cleanup routine too. Garbage-collected languages such as Java and C# handle memory cleanup themselves, so the pairing is less direct there.

Where to start

If the idea of a class is still new, start with what are classes in c++. A constructor only makes sense once you know what a class is: a blueprint that describes the data and functions that every object made from it will have. Once that clicks, the constructor is simply the part of the blueprint that runs when each object is created.

The complete guide to constructors

Class Constructors in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Struct vs Class in C++

In C++, struct and class differ mainly in default access: struct members are public, class members are private. See when to use each, with examples.