# Constructor

> A method that runs when a new object is made. It sets the first values.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** __init__, Constructor Method, Initializer

## Think of it like

Think of it like a name tag. You fill it in the day a new pet comes home.

## Definition

A constructor is a special method that runs once, when a new object is made. Its job is to set up the object's first values, like a name or a score of zero. In Python it is called __init__; in JavaScript and Java it is called constructor. Whatever you pass when you make the object goes to it. Without one, the object starts with nothing set.

## Example

When Emma writes Dog("Rex"), the constructor runs and saves Rex as the dog's name.

## Example code

```python
class Dog:
    def __init__(self, name):
        self.name = name
```

__init__ runs once for each new dog and stores its name.

## Why it matters

It makes sure every object starts in a valid state. No dog exists without a name.

## Common mistake

Doing slow work, like reading files, inside the constructor. Making each object then becomes slow.

## Three principles

- **Runs once.** When the object is made.
- **Sets starting values.** From what you pass in.
- **Different names.** __init__ or constructor.

## Related terms

- [Class](https://learn.benibix.com/term/development/class)
- [Object (Programming)](https://learn.benibix.com/term/development/object)
- [Method (Programming)](https://learn.benibix.com/term/development/method)
- [Parameter](https://learn.benibix.com/term/development/parameter)

---

Source: https://learn.benibix.com/term/development/constructor
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
