# 9.3 Constructors and New

> `New` creates an object and runs its constructor, which prepares the attributes before anyone uses it.

Web page: https://pseudolearn.app/en/spec/objetos/constructor

> **In one sentence**
>
> `New` creates an object and runs its constructor, which prepares the attributes before anyone uses it.

## 9.3.1 How to write it

```pseudolearn
Method Constructor(«parámetros»)
  «instrucciones»
EndMethod
New «Clase»(«argumentos»)
```

> **Parts**
>
> - `Method Constructor` declares the constructor. It has no visibility or return type.
> - Every class has **at most one**. If none is written, there is an implicit one without parameters that sets nothing.
> - `New` followed by the class name and the arguments creates the object. The parentheses are required.

## 9.3.2 How it works

Declaring a class-typed variable **creates no object**: the variable stays without a value until it is assigned one created with `New`.

**Figure 9.3-1: Creating an object**

The variable pasos starts empty. An object with the attribute valor is reserved, the constructor gives it 10 and finally pasos receives the reference to the object.

What happens when `New Contador(10)` is evaluated.

1. Step 1: A new object is reserved with its attributes without a value.
2. Step 2: The constructor runs with `inicial` = 10, and `This.valor` receives 10.
3. Step 3: The reference to the object is stored in the variable.

### Program: A constructor that prepares the object

```pseudolearn
Class Contador
  Private Define valor As Integer;

  Method Constructor(inicial As Integer)
    This.valor <- inicial;
  EndMethod

  Method Actual() As Integer
    Return This.valor;
  EndMethod
EndClass

Algorithm Crear
  Write New Contador(10).Actual();
EndAlgorithm
```

**Output**

```text
10
```

*The program finishes normally.*

If the class inherits, the superclass part is built before the constructor body. When the superclass constructor needs arguments, the first statement must be `Super.Constructor(…)`.

> **Common mistakes**
>
> A class variable without `New` has no object:
>
> ### Program: Using an object that was never created
>
> ```pseudolearn
> Class Punto
>   Define x As Integer;
> EndClass
>
> Algorithm SinNuevo
>   Define p As Punto;
>   p.x <- 1;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `variableUsedUninitialized` (error, 7:3): Variable p may have no value at this point (cause: no earlier path assigns it a value)
>
> *The program finishes with an error.*

> **Under the hood**
>
> `New` is a reservation instruction followed by a call to the constructor with the freshly created object as receiver.
>
> **Intermediate representation**
>
> ```text
> module Crear en
> class Contador { valor: i64 }
> func suspendible @main() -> unit {
> b0() mass 1:
>   stmt.enter @14:3-14:34
>   %0 = const.i64 10 @14:22-14:24
>   %1 = object.new Contador @14:9-14:25
>   %2 = call @Contador.Constructor(%1, %0) @14:9-14:25
>   %3 = call @Contador.Actual(%1) @14:9-14:34
>   out.write %3, newline @14:3-14:34
>   stmt.exit @14:3-14:34
>   return @15:1-15:13
> }
> func @Contador.Constructor(%0: obj<Contador>, %1: i64) -> unit {
> b0() mass 1:
>   stmt.enter @5:5-5:26
>   object.store %0, valor, %1 @5:5-5:26
>   stmt.exit @5:5-5:26
>   return @6:3-6:12
> }
> func @Contador.Actual(%0: obj<Contador>) -> i64 {
> b0() mass 1:
>   stmt.enter @9:5-9:22
>   %1 = object.load %0, valor @9:12-9:22
>   stmt.exit @9:5-9:22
>   return %1 @9:5-9:22
> }
> ```

**In short**
- `New Class(…)` creates an object and runs its constructor.
- There is at most one constructor; without it, an implicit one that initializes nothing.
- Declaring a class variable does not create the object.
- The superclass is built first.

**See also**
- [9.1 Classes and attributes](https://pseudolearn.app/en/spec/objetos/clase.md)
- [9.4 Inheritance and overriding](https://pseudolearn.app/en/spec/objetos/herencia.md)
