Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

solidity
communitycreator

What are constructors in Solidity?

Shubham Singh Kshatriya

Overview

When creating a Solidity smart contract, we can declare a special function called a constructor. A constructor contains the initialization logic of state variables.

In other object-oriented programming languages, a constructor is invoked whenever an object of a class is instantiated. A constructor can be invoked only when the smart contract is deployed in Solidity. After it’s deployed, there is no way to invoke the constructor again.

If a constructor is not defined explicitly, the compiler will create a default constructor.

Constructor declaration

To declare a constructor, we use the constructor keyword followed by the access specifier. Inside the {} brackets, we add the initialization logic.

A constructor’s access specifier can be public or internal. A contract with an internal constructor is marked as abstract and cannot be deployed.

constructor() <access-specifier> {
    // code
}
Syntax of constructor declaration

In the previous version of Solidity (^0.4.*), in order to declare a constructor, we defined a function with the same name as the constructor. But we don’t use it anymore because it’s deprecated.

Example 1

Let’s see an example of a constructor in Solidity.

pragma solidity ^0.5.0;

contract Example {
  // declare a state variable
  uint _a;

  // create a constructor to initialize _a
  constructor (uint a) public {
    _a = a;
  }

  // getter function to get value of _a
  function getA() public view returns(uint){
    return _a;
  }
}
Constructor example

Explanation

  • Line 3: We create a contract named Example.
  • Line 5: We declare a state variable _a.
  • Line 8: We create a constructor that initializes the value of _a.
  • Line 13: We create a getter function getA to retrieve the value of _a.

Constructor inheritance

While inheriting a contract, if the parent contract has a constructor with some parameters, the child contract needs to provide those same parameters. If the child contract doesn’t pass the required parameters to the parent contract’s constructor, it will be marked as abstract and will not be deployed.

If no explicit constructor is defined in the parent contract, its default constructor will be called.

constructor () <parent-contractName>() <access-specifier>{
    // code
}
Syntax of constructor inheritance

Example 2

Let’s see an example of constructor inheritance.

pragma solidity ^0.5.0;

contract ParentContract {
  // declare a state variable
  uint _a;

  // create a constructor
  constructor (uint a) internal {
    _a = a;
  }

  // getter function to get value of _a
  function getA() public view returns(uint){
    return _a;
  }
}

// inherit ParentContract
contract ChildContract is ParentContract {
  // declare a state variable
  uint _b;

  // create a constructor and call parent constructor
  constructor (uint a, uint b) ParentContract(a) public {
    _b = b;
  }

  // getter function to get value of _b
  function getB() public view returns(uint){
    return _b;
  }
}

Explanation

  • Line 3: We create a contract named ParentContract.
  • Line 5: We declare a state variable _a.
  • Line 8: We create a constructor that initializes the value of _a.
  • Line 13: We create a getter function getA() to retrieve the value of _a.
  • Line 19: We inherit a contract named ChildContract from ParentContract.
  • Line 21: We declare a state variable _b.
  • Line 24: We create a constructor that initializes the value of _b. We also call the constructor of the parent contract and pass the required parameter.
  • Line 29: We create a getter function getB() to retrieve the value of _b.

RELATED TAGS

solidity
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya
RELATED COURSES

View all Courses

Keep Exploring