DEV Community

Cover image for Constructor in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Constructor in Solidity

In Solidity, a constructor is a special function that is used to initialize the state variables of a smart contract

A constructor in Solidity is a special function declared using the constructor keyword. It is an optional function used to initialize the state variables of a contract. A contract can have only one constructor, and it is executed once the contract is first created. Constructors can be either public or internal, and if no constructor is defined, a default constructor will be present in the contract. Constructors are useful for setting parameter values at run time and restricting method calls. Constructor overloading is not supported in Solidity.

In Solidity, a constructor is a special function that is called only once at the time of contract creation. The constructor function is used to initialize the state variables of a smart contract. It is defined using the constructor keyword, and it is optional. If a constructor is not defined, the contract is initialized with the default values of its state variables.

The constructor function is called when the contract is deployed to the blockchain network. It has the same name as the contract and it can take input parameters. The constructor can be used to set the initial values of the contract's state variables, and it can also be used to perform certain checks to ensure that the contract is deployed in a valid state. Once the constructor function is executed, it cannot be called again.

Example of a constructor function:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleContract {
    string public message;

    constructor(string memory _message) {
        message = _message;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the SimpleContract has a single-state variable message, which is initialized using the constructor function. The constructor the function takes a string input parameter _message, which is used to set the value of the message variable. When the contract is deployed, the constructor is called with the input parameter and the message the variable is initialized with the value of the parameter.

When a contract is created, its constructor (a function declared with the constructor keyword) is executed once. A constructor is optional. Only one constructor is allowed, which means overloading is not supported.

When to use a constructor?

After the constructor has been executed, the final code of the contract is stored on the blockchain. This code includes all public and external functions and all functions that are reachable from there through function calls. The deployed code does not include the constructor code or internal functions only called from the constructor.

Internally, constructor arguments are passed ABI encoded after the code of the contract itself, but you do not have to care about this if you use web3.js.

If a contract wants to create another contract, the source code (and the binary) of the created contract has to be known to the creator. This means that cyclic creation dependencies are impossible.

Solidity Constructor example:

// Solidity program to demonstrate
// creating a constructor
pragma solidity ^0.8.0;       

// Creating a contract
contract constructorExample {       

    // Declaring state variable
    string str;       

    // Creating a constructor
    // to set value of 'str'
    constructor() public {                 
        str = "Hello";       
    }       

    // Defining function to
    // return the value of 'str' 

    function getValue(
    ) public view returns (
      string memory) {       
        return str;       
    }     
} 
Enter fullscreen mode Exit fullscreen mode

 For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)