In this Solidity tutorial, we will learn how to create and use a Solidity Constructor with the help of multiple examples. Additionally, we will cover the following set of topics.
- Solidity Constructor
- Solidity Constructor Syntax
- Solidity Constructor Example
- Solidity Constructor Arguments
- Solidity Constructor Visibility
- Solidity Constructor Inheritance
- Solidity Constructor Payable
- Solidity Constructor Overloading
- Solidity Constructor Public
- Solidity Constructor Array
- Solidity Constructor Super
- Solidity Constructor Abstract
Solidity Constructor
A constructor is a very popular notion in object-oriented programming. It is a special function that we usually define within a class in many programming languages.
Moreover, this function will be executed once when a new object is created. Solidity also supports the use of constructors. But, a constructor in Solidity is defined with a smart contract, and its executed when a smart contract is deployed.
In Solidity, the main role of a constructor is to initialize the state of the smart contract. Now, if a constructor function is not defined explicitly in a contract the Solidity compiler will automatically create one by default.
Read: Solidity Function + Examples
Solidity Contractor Syntax
Now that we understood what are constructors in Solidity let us discuss how to create them in Solidity. For this task, we use the constructor keyword in Solidity. Here is the complete syntax of defining a constructor in Solidity.
contract contract_name{
constructor() access_modifier {
// body_of_the_constructor
}
}
In the syntax, first, we need to define a contract, and when within the contract, we can define a constructor using the constructor keyword.
After the constructor keyword, led by an access modifier. And in the last, we need to specify the body of the constructor within the curly brackets.
Note: As constructor is a special function, it is not required to use the function keyword while dfrining it.
Solidity Constructor Example
Now that we understand how to define constructors in Solidity let us illustrate an example related to constructors. And the code for the example is as follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract SampleConstruct {
string country_name;
constructor() {
country_name = "United States";
}
function getCountryName() public view returns (string memory) {
return country_name;
}
}
- In the above example, first, we will declare a contract named SampleConstruct. Within this contract, we declared a string variable named country_name.
- After the variable, we defined a constructor and within the body of the constructor, we set the value of country_name as the United States.
- In the last, we created a function that will return the value of od country_name variable.
So, initially, we will deploy the contract and call the function, and the value of the variable will be automatically set to the United States.
Here is the result of the above contract once we deploy the contract.

Read: Solidity String – Complete tutorial
Solidity Constructor Overloading
Any object-oriented programming language comes with a feature of function overloading. This function overloading feature allows defining multiple functions with the same name but with distinct parameters. And the compilers can easily differentiate these functions with the same and different functionality.
Now, constructors are also just functions that are executed automatically. Many programming languages also support constructor overloading where we can define multiple constructors having different parameters.
However, in the case of Solidity, we can only define one constructor at a time. And we cannot define more than one constructor in our contract. So, Solidity does not support the implementation of constructor overloading.
And even if we try to implement more than one constructor in a smart contract, the Solidity compiler will return an error. Here is an example.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract SampleConstruct {
string country_name;
constructor() {
country_name = "United States";
}
constructor() {
country_name = "Canada";
}
function getCountryName() public view returns (string memory) {
return country_name;
}
}
So, for the execution, we simply added one more constructor in our previous example. And at the time of deployment, the Solidity compiler will return an error.

Solidity Constructor Arguments
In this section, we will illustrate how to declare a constructor in Solidity with arguments. Till now, we have only seen how to create constructors in Solidity without any argument. However, just like other programming languages like C++ & JAVA, Solidity also supports constructors with arguments.
To define parameters with a constructor, we need to specify the parameter values with the parenthesis of the constructor function.
contract contract_name{
constructor(type param_1, type param_2, ....) {
// body_of_the_constructor
}
}
In the above syntax, just need to specify the parameters with their data type in the parenthesis of the constructor. Let us understand the execution using an example in Solidity.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract SampleConstruct {
int price;
constructor(int setPrice) {
price = setPrice;
}
function getPrice() public view returns (int) {
return price;
}
}
- In the above code, first, we have created a contract named SampleContruct. Within the contract, we have declared an integer variable price.
- Afte variable, we defined a constructor that accepts a single integer parameter setPrice.
- In the instructor body, we are setting the value of price as setPrice. In the end, we created another function named getPrice which will return the value of the price variable as a result.
Here is the execution of the above smart contract.

Read: Introduction to Solidity
Solidity Constructor Public
In this section, we will understand the need of declaring a constructor as public. So, for the Solidity version lower than 0.7, we need to specify the access specifiers for constructors as public or external.
But, with the introduction of Solidity version 0.7, the Solidity team has removed this feature of defining a constructor as public or external. And if we try to define the constructor as public, the compiler will return an error with the following message.

So, to resolve this error in Solidity, we simply need to remove the public keyword from the above code. As it is not required to define a constructor as public within a smart contract.
Here is the sample code and its output.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract SampleConstruct {
int price;
constructor(int setPrice) {
price = setPrice;
}
function getPrice() public view returns (int) {
return price;
}
}
The above example is the same as given in the “Solidity Constructor Arguments” section. For information, check the example explained in the given section. In the last, we will get the following result.

Solidity Constructor Array
In this section, we will discuss how to utilize an array within a constructor in a Solidity smart contract. But, before that, we all should have some basic understanding of arrays in Solidity.
An array in Solidity is a group of data elements stored in the same memory region at the same time. We can also consider it as a basic data structure where each data element may be accessed simply by its index value. For more detail, please check the following tutorial on Solidity Arrays.
Now, in Solidity, we cannot directly pass an array as an argument. But, we can perform some array operations within the constructor body. Let us understand this thing with the help of an example. And the code for the example is as follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract SampleConstruct {
string[] stocks;
constructor(string memory stock_name) {
stocks.push(stock_name);
}
function getStockList() public view returns (string[] memory) {
return stocks;
}
}
- In the above code, first, we created a contract named SampleConstruct.
- Now, within the contract, we defined a public string type array named stocks.
- After this, we defined a constructor where we are pushing the stock_name parameter to the stocks array.
- In the last, we defined a function named getStockList which will return the value of the entire array.
Here is the execution of the above given st=mart contract.

At the time of deployment, we have given the value of stock_name as the United States Steel. Now, when we deploy the contract and run the getStockList function, we can observe that the stocks array consists of the given value.
Solidity Constructor Payable
When developing a smart contract, we need to ensure that ETHER is transmitted to the contract as well as out of the contract. For this task, we need to define a function in Solidity with the modifier Payable.
Moreover, this ensures the function’s ability to send and receive Ethers. And same is the case with constructors. So, we can also define a constructor as payable, if we want to send or receive some ETHERS using the constructor.
Let us understand this concept using an example in Solidity. And the code for this implementation is given below.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract Withdraw{
address payable public Owner;
constructor (uint _amount) payable {
Owner = payable(msg.sender);
Owner.transfer(_amount);
}
}
In the above example, first, we created a smart contract named Withdraw. Within the contract, we defined an Owner variable of type payable address.
After this, we defined a constructor that will accept the _amount integer value and withdraws it from Owner’s address.
The execution of the above smart contract is given below.

In the execution, we have defined some value in the address. In the example, we have specified this value as 3000. After this, we have to pass some integer value while deploying the contract. In the above execution, we specified this value as 21.
Solidity Constructor Inheritance
In this section, we will illustrate how to inherit a constructor in Solidity. One important concept in OOPS is the use of inheritance.
Just like biological inheritance, the inheritance in Solidity enables developers to create objects whose behavior can be inherited from other objects. Inheriting behavior in Solidity means accessing variables and objects of the parent contract in the child or derived contract.
So, here we will illustrate how to inherit a constructor from a parent contact to the child contract. And we will understand this concept in Solidity using an example.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract BaseContract {
uint val;
constructor(uint _val) {
val = _val;
}
function PrintVal() public view returns(uint){
return val;
}
}
contract ChildContract is BaseContract(37){
constructor() {}
function getData() public view returns(uint){
uint result = val * 2;
return result;
}
}
- In the above execution, first, we created a smart contract named BaseContract.
- Within this, we declared a unit type variable named val and after this, we defined a constructor using which we will set the value of val.
- In the last of the contract, we defined a function PrintVal() which will return the value of val variable.
- After the BaseContract, we defined another contract named ChildContract which inherit the BaseContract.
- In this contract, we defined an empty constructor and then we defined another function named getData() which will return a result variable.
- The value of the result variable will be calculated by multiplying the value of val by 2.
Here is the execution of the above smart contract.

Solidity Constructor Super
In Solidity, the super keyword is utilized in inheritance to call a function from the Parent contract within the Derived contract. In the case of a constructor, we don’t require to call a constructor using the super keyword. This is because a constructor in a contract gets executed automatically whenever we deploy a contract.
However, if we want, we can call the constructor of a parent contract in the child contract. For this, we simply need to call the Parent contract in the child contract constructor.
Let us understand this implementation using an example in Solidity. And the code for the examples is as follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract BaseContract {
uint val;
constructor(uint _val) {
val = _val;
}
function PrintVal() public view returns(uint){
return val;
}
}
contract ChildContract is BaseContract{
constructor() BaseContract(12) {
}
function getData() public view returns(uint){
uint result = val;
return result;
}
}
In the above example, we created 2 contracts – BaseContract and ChildContract respectively. And the ChildContract inherits BaseContract. Now, within the ChildContract, we defined a constructor and with the constructor, we are calling the BaseContract with constructor input as 12.
The execution of the above Solidity program is given below.

Solidity Constructor Abstract
In Solidity, we generally declare a contract as abstract when it consists of one or more functions without any implementations. But, we cannot define a constructor as abstract in Solidity. And even if we try to define a constructor in Solidity as abstract, the Solidity compiler will return an error.
Solidity Constructor Visibility
For visibility, before Solidity version 0.7, we need to define the constructor either as public or as internal. But with Solidity version 0.7 or higher, we don’t need to specify the visibility of the constructor in Solidity. However, if we try to specify the visibility using the public or internal keyword in Solidity version 0.7 or higher, the compiler will return a warning.
So, in this Solidity tutorial, we understood how to create and use a Solidity Constructor with the help of multiple examples. Additionally, we covered the following set of topics.
- Solidity Constructor
- Solidity Constructor Syntax
- Solidity Constructor Example
- Solidity Constructor Arguments
- Solidity Constructor Visibility
- Solidity Constructor Inheritance
- Solidity Constructor Payable
- Solidity Constructor Overloading
- Solidity Constructor Public
- Solidity Constructor Array
- Solidity Constructor Super
- Solidity Constructor Abstract
I am Bijay, a Microsoft MVP and founder of TSInfo Technologies, a SharePoint development company. Currently focusing on getting expertise on Ethereum, Solidity, Bitcoin, Cryptocurrency, Blockchain, etc. Sharing my expertise and tutorials on Bitcoin and Ethereum related technologies. Read More…