Solidity Constructors

In this article, we will dive into Solidity constructors, their syntax, and how they are used to create smart contracts on the Ethereum blockchain.

In any object-oriented programming language, a constructor is the name of the special method that gets invoked whenever an object of that class is initialized.

For Solidity, it’s totally different. Solidity has a constructor declaration in the smart contract which is invoked only once when the contract is deployed and used to initialize the contract state.

Compilers create default constructors if they are not explicitly defined.



What are Constructors in Solidity?

A constructor is a special function in Solidity that is executed when a contract is created.

It is used to initialize the state variables of the contract.

The constructor has the same name as the contract and is defined using the keyword constructor.


How do Constructors work in Solidity?

Constructors are executed when a contract is deployed to the blockchain. When a user creates an instance of the contract, the constructor is called to initialize the state variables of the contract.

The arguments to the constructor are passed by the user at the time of deployment.

Solidity constructor can have any number of arguments and can perform any operation that is valid.

Once the constructor is executed, the state variables of the contract are initialized and the contract is ready to be used.

Note: The constructor will not called again during the lifetime of the contract.

Solidity Constructor Creation

The constructor keyword is used without any function name and is followed by an access modifier.

This is an optional function that initializes the contract’s state variables.

It is possible for a constructor to be either internal or public, and an internal constructor marks the contract as abstract.

Syntax

The syntax of a constructor is shown as below:

constructor() <Access Modifier> {
}

The example below shows the method to create a constructor:

Example: 

pragma solidity ^0.5.0;// This is the Parent Contract //contract SolidityTest {string public mrx;constructor() public{mrx="Mr.Examples";}function get_Name() public view returns(string memory){return mrx; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Use Constructor with Argument

A constructor in solidity can also have several arguments.

The given code provides the understanding of using a constructor with arguments:

Example: 

pragma solidity ^0.5.0;// This is the Parent Contract //contract SolidityTest {string public mrx_name="Mr.Examples"; uint public age=21; string public description="Visit Mr Examples !! if you want to learn knowledgeable facts regarding Programming Languages";constructor(string memory mrx,uint mrx_age,string memory desc) public{mrx=mrx_name; mrx_age=age; desc=description;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
The following example provides better understanding regarding solidity parameterized constructor:

Example: 

pragma solidity ^0.5.0; // Creating a contractcontract SolidityTest{string public mrx="Mr.Examples"; address public mrx_1=msg.sender; constructor(string memory ample,address ample_1) public {ample=mrx; ample_1=mrx_1;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Constructor Inheritance

The following is the method of using constructor during the process of inheritance:

Example: 

pragma solidity ^0.8.0; contract SolidityTest { // This is the parent contract uint256 public mrx;constructor(uint256 _mrx) { mrx = _mrx; } }contract SolidityTest1 is SolidityTest { // This is the first Inherited Contract uint256 public ample;constructor(uint256 _mrx, uint256 _ample) SolidityTest(_mrx) { ample = _ample; } }contract SolidityTest2 is SolidityTest { // This is the second child class inherited from first child contract uint256 public mrx_ample;constructor(uint256 _mrx, uint256 _mrx_ample) SolidityTest(_mrx) { mrx_ample = _mrx_ample; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Importance of Constructors in Solidity

Constructors are an essential part of Solidity programming. They are used to initialize the state variables of the contract, which are the variables that store the data for the contract.

Please Note: If the state variables are not initialized properly, the contract may not function as intended.

Constructors also allow users to provide input to the contract when it is created.

This input can be used to customize the behavior of the contract or to set initial values for the state variables.


Conclusion

Solidity constructors allow us to initialize state variables in our contracts and to perform any necessary setup logic when the contract is deployed, in order to perform the initialization of state variables in our contracts.

This means that constructors are capable of taking any number and combination of arguments, and they can be used to perform any initialization logic in order to make a contract function as intended.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *