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> {
}
Example: In the below example, in the contract constructorExample, a constructor is created to initialize the state variable str.
Solidity
pragma solidity ^0.5.0;
contract constructorExample {
string str;
constructor() public {
str = "GeeksForGeeks" ;
}
function getValue(
) public view returns (
string memory) {
return str;
}
}
|
Output :

Constructor in Inheritance
In case if a constructor is not defined then the default constructor is called, but if the constructor is defined in a parent contract and having some arguments then the child contract should also provide the required parameters to the constructor. If the child contract is not passing any parameter to the parent’s constructor the child contract will become an abstract contract. There are two ways of calling a parent contract’s constructor:
1. Direct Initialization: In the below example, the direct initialization method is used to initialize the constructor of the parent class.
Solidity
pragma solidity ^0.5.0;
contract Base {
uint data;
constructor(uint _data) public {
data = _data;
}
function Print(
) public returns(string memory){
return "Direct Initialization" ;
}
}
contract Derived is Base(2){
constructor() public {}
function getData(
) external returns(uint){
uint result = data ** 2;
return result;
}
}
contract caller{
Derived c = new Derived();
function getResult() public returns(uint){
c.Print();
return c.getData();
}
}
|
Output :

2. Indirect Initialization: In the below example, the indirect initialization using Base(string(abi.encodePacked(_info, _info))) is done to initialize the constructor of the base class.
Solidity
pragma solidity ^0.5.0;
contract Base {
string str;
constructor(
string memory _str) public {
str = _str;
}
function Print(
) public returns(string memory){
return "Indirect Initialization" ;
}
}
contract Derived is Base {
constructor(
string memory _info) Base(
string(abi.encodePacked(
_info, _info))) public {}
function getStr(
) public view returns(string memory){
return str;
}
}
contract caller {
Derived c
= new Derived( "GeeksForGeeks" );
function getResult() public view{
c.Print();
c.getStr();
}
}
|
Output :

Need of Constructors
Constructors are very useful in a smart contract, a parameter value can be defined at the run time and can also restrict the method call. Constructor overloading is not supported in Solidity, it only allows one constructor at a time.
Example: In the below example, the contract constructorExample consists of a constructor to demonstrate the need for the constructor.
Solidity
pragma solidity ^0.5.0;
contract constructorExample {
string str;
address private owner
= 0x62Ab98A0efB752C48bd82836D0b4335018B9B97e;
constructor(string memory string) public {
if (msg.sender == owner){
str = string;
}
}
function getValue() public view returns (
string memory) {
return str;
}
}
|
Output :
