Constructor In Solidity

constructor-in-solidity
 
Constructor is a special method which gets invoked whenever an instance of a class is created – that is correct for object-oriented programming languages. However, in Solidity, it’s different; Solidity supports declaring constructor inside a smart contract and it is invoked—only once—while deploying it. The compiler creates default constructor if there is no constructor defined explicitly.
 

Constructor Declaration

 
The constructor is declared by keyword “constructor” with no function name, followed by access modifier - public or internal. Solidity doesn’t allow us to create a private or external constructor.
  1. constructor() <Access Modifier> {          
  2. }  
Constructor Example
  1. pragma solidity ^0.5.0;        
  2.         
  3. contract SolidityByExample {        
  4.         
  5.     uint value;        
  6.             
  7.     constructor() public {                  
  8.         value = 1;        
  9.     }        
  10.             
  11.     function getValue() public view returns (uint) {        
  12.         return value;        
  13.     }      
  14. }   
Deploying the above contract will execute the constructor and set value 1 to the “value” variable.
 
Remix Contract Deployment 
 
Now, to check if the constructor was executed properly or not, we will try to retrieve the value of our variable. So, let’s call the method and check the output. We should see output 1 here.
 
Remix Contract Result 
 

Use of Constructor

 
Constructors are helpful in many ways. You can set parameter value run time and restrict the method calling. For instance, assume that I’m creating a smart contract to create my own tokens, and I want to define total supply while deploying the contract. I can easily do that using constructor. 
  1. pragma solidity ^0.5.0;    
  2.     
  3. contract MyToken {    
  4.     
  5.     int totalSupply;    
  6.         
  7.     constructor(int _totalSupply) public {    
  8.         totalSupply = _totalSupply;    
  9.     }    
  10.         
  11.     function getTotalSupply() public view returns (int) {    
  12.         return totalSupply;    
  13.     }     
  14. }  
But if you notice, here is one issue though, anyone can deploy this contract. But I want to restrict it for specific users (addresses) only. Let’s extend this code to the next level. 
  1. pragma solidity ^0.5.0;    
  2.     
  3. contract MyToken {    
  4.     
  5.     int totalSupply;    
  6.     address private owner = 0x4B0897b0513fdC7C541B6d9D7E929C4e5364D2dB;    
  7.         
  8.     constructor(int _totalSupply) public {    
  9.             
  10.         if(msg.sender == owner){    
  11.             totalSupply = _totalSupply;    
  12.         }    
  13.     }    
  14.         
  15.     function getTotalSupply() public view returns (int) {    
  16.         return totalSupply;    
  17.     }     
  18. }  
In this example, the total supply will only set if the contract is deployed by a specified address. Let’s run and check it out. First I will deploy it using some other address.
 
Contructor in Solidity
  
Things to notice
  • The address of executor, ends up with 733c
  • Total Supply amount is 10, which was passed during deployment
  • Total Supply value is still not updated as the contract was not executed from the address defined in the contract.
Now, let’s change the executor address and deploy the contract again.
 
Solidity Constructor 
 
Things to notice
  • The address of executor is the same as we defined in the contract
  • Total Supply amount is 10
  • Total Supply value was updated to 10 as our condition was met

Constructor Overloading

 
Solidity doesn't support constructor overloading. It allows one constructor at a time. The following code will give an error.
  1. constructor(int value 1) public {                      
  2. }          
  3.       
  4. constructor(int value2, int value2) public {                      
  5. }  
In this article, we learned about Solidity constructor and the use of it. Keep in mind, we have used “if condition” to restrict some code, just for understanding. Solidity has different inbuilt functions like require, revert, and assert for input validation, that we will learn in upcoming articles.


Similar Articles