What are constructors in Solidity?
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}
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 variableuint _a;// create a constructor to initialize _aconstructor (uint a) public {_a = a;}// getter function to get value of _afunction getA() public view returns(uint){return _a;}}
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
getAto 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}
Example 2
Let’s see an example of constructor inheritance.
pragma solidity ^0.5.0;contract ParentContract {// declare a state variableuint _a;// create a constructorconstructor (uint a) internal {_a = a;}// getter function to get value of _afunction getA() public view returns(uint){return _a;}}// inherit ParentContractcontract ChildContract is ParentContract {// declare a state variableuint _b;// create a constructor and call parent constructorconstructor (uint a, uint b) ParentContract(a) public {_b = b;}// getter function to get value of _bfunction 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
ChildContractfromParentContract. - 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.