Skip to content

Latest commit

 

History

History
71 lines (48 loc) · 2.58 KB

File metadata and controls

71 lines (48 loc) · 2.58 KB

30 Days Of Solidity: Constructors

Twitter Follow

Author: Vedant Chainani
June, 2022

<< Day 19 | Day 21 >>

Cover


📔 Day 20

A constructor is a special method in any object-oriented programming language which gets called whenever an object of a class is initialized. It is totally different in case of Solidity, Solidity provides a constructor declaration inside the smart contract and it invokes only once when the contract is deployed and is used to initialize the contract state. A default constructor is created by the compiler if there is no explicitly defined constructor

Creating a constructor

A Constructor is defined using a constructor keyword without any function name followed by an access modifier. It’s an optional function which initializes state variables of the contract. A constructor can be either internal or public, an internal constructor marks contract as abstract.

Syntax:

constructor() <Access Modifier> {
}

Following are the key characteristics of a constructor.

  • A contract can have only one constructor.

  • A constructor code is executed once when a contract is created and it is used to initialize contract state.

  • After a constructor code executed, the final code is deployed to blockchain. This code include public functions and code reachable through public functions. Constructor code or any internal method used only by constructor are not included in final code.

  • A constructor can be either public or internal.

  • A internal constructor marks the contract as abstract.

Example:

pragma solidity ^0.8.7;

contract constructorExample {
    string str;

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

    // Defining function to return the value of 'str'
    function getValue() public view returns (string memory) {
        return str;
    }
}

<< Day 19 | Day 21 >>