Skip to content

The Solidity Constructor

A constructor is a function that is called once during deployment of the Smart Contract. It's also called once only and can't be called again afterwards.

This lets you set specific things in your Smart Contract during deployment. For example, you could specify who deployed the Smart Contract - and then let only that person destroy the Smart Contract. Like an owner. Let's try this!

//SPDX-License-Identifier: MIT

pragma solidity 0.8.3;

contract FunctionsExample {

    mapping(address => uint) public balanceReceived;

    address payable public owner;

    constructor() {
        owner = payable(msg.sender);
    }

    function destroySmartContract() public {
        require(msg.sender == owner, "You are not the owner");
        selfdestruct(owner);
    }

    function receiveMoney() public payable {
        assert(balanceReceived[msg.sender] + msg.value >= balanceReceived[msg.sender]);
        balanceReceived[msg.sender] += msg.value;
    }

    function withdrawMoney(address payable _to, uint _amount) public {
        require(_amount <= balanceReceived[msg.sender], "not enough funds.");
        assert(balanceReceived[msg.sender] >= balanceReceived[msg.sender] - _amount);
        balanceReceived[msg.sender] -= _amount;
        _to.transfer(_amount);
    } 

    receive() external payable {
        receiveMoney();
    }
}

What does it do?

  1. In the constructor it sets the owner address to the address who deployed the Smart Contract
  2. Upon destruction, it checks who called the function.
    1. If the owner called, then the Smart Contract is destroyed
    2. If it is not the owner, then an error is thrown

Running the Smart Contract

Let's try this!

  1. Deploy the Smart Contract
  2. Check the owner address: you should see the same account as selected!
  3. Destroy the Smart Contract
  4. Click on the owner address again: it's zero now, because the Smart Contract is not longer existing.

It should work fine! No error.

Before hitting Destroy:

After hitting Destroy:

Executing the Constructor

The constructor is executed only during deployment. There is no way to execute the constructor code afterwards.

If you look at the interface of your Smart Contract (the buttons on the left side), there is "destroySmartContract", "receiveMoney", "withdrawMoney", "balanceReceived" and "owner", but no way to call the constructor code again. This is intentional and a feature of Solidity.

But what about the require statement?

Let's deploy a new version of the Smart Contract, but this time try to call destroySmartContract with a different account:

  1. Deploy Smart Contract
  2. Select a different account from the Accounts Dropdown
  3. Hit the "destroySmartContract" Button

It should give you an error.

Do you see the different Account when you hit "owner"?

Summary

Why is that so powerful?

We automatically set a variable depending on who deployed the Smart Contract. You can also have constructor arguments, just like any other function. It will be executed once and once only.