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. contract SolidityByExample {
  3. uint value;
  4. constructor() public {
  5. value = 1;
  6. }
  7. function getValue() public view returns (uint) {
  8. return value;
  9. }
  10. }
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. contract MyToken {
  3. int totalSupply;
  4. constructor(int _totalSupply) public {
  5. totalSupply = _totalSupply;
  6. }
  7. function getTotalSupply() public view returns (int) {
  8. return totalSupply;
  9. }
  10. }
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. contract MyToken {
  3. int totalSupply;
  4. address private owner = 0x4B0897b0513fdC7C541B6d9D7E929C4e5364D2dB;
  5. constructor(int _totalSupply) public {
  6. if(msg.sender == owner){
  7. totalSupply = _totalSupply;
  8. }
  9. }
  10. function getTotalSupply() public view returns (int) {
  11. return totalSupply;
  12. }
  13. }
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
Now, let’s change the executor address and deploy the contract again.
Solidity Constructor
Things to notice

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. constructor(int value2, int value2) public {
  4. }
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.