Constructors
A constructor is a special function that runs exactly once, when the contract is deployed. It sets up the initial state: the owner, configuration values, initial balances. Afterwards its code is thrown away; it is not part of the deployed bytecode and can never be called again.
constructor(string memory name_, uint256 supply_) {
name = name_;
totalSupply = supply_;
owner = msg.sender;
}A contract has at most one constructor, and it needs no visibility keyword. If you don't write one, the contract gets an empty default constructor.
Constructor arguments
Arguments are passed at deployment. From Solidity, that is new Contract(arg1, arg2):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Membership {
string public clubName;
uint256 public maxMembers;
address public founder;
uint256 public createdAt;
constructor(string memory name_, uint256 max_) {
require(max_ > 0, "club needs room for members");
clubName = name_;
maxMembers = max_;
founder = msg.sender;
createdAt = block.timestamp;
console.log("constructor ran for", name_);
}
}
contract Main {
function main() external {
Membership chess = new Membership("Chess Club", 30);
console.log("name :", chess.clubName());
console.log("max :", chess.maxMembers());
console.log("founder:", chess.founder());
console.log("created:", chess.createdAt());
}
}constructor ran for Chess Club
name : Chess Club
max : 30
founder: 0x5FbDB2315678afecb367f032d93F642f64180aa3
created: 1767225600
The trailing underscore in name_ is a convention to avoid shadowing the state variable name.
A failing constructor
If a constructor reverts, the contract is not deployed at all. That makes the constructor the right place to validate configuration:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Membership {
uint256 public maxMembers;
constructor(uint256 max_) {
require(max_ > 0, "club needs room for members");
maxMembers = max_;
}
}
contract Main {
function main() external {
Membership ok = new Membership(10);
console.log("first club max:", ok.maxMembers());
new Membership(0); // reverts: nothing is deployed
console.log("not reached");
}
}first club max: 10
main() failed: reverted with reason: club needs room for members
The owner pattern
The most common constructor line in Ethereum is owner = msg.sender;. Whoever deploys the contract becomes its administrator. Functions can then check msg.sender == owner (the Modifiers lesson turns that into a reusable onlyOwner).
payable constructors
Mark the constructor payable to let the deployment transaction carry ether. In this playground, a payable constructor of Main automatically receives 100 ETH, which is how the examples get funds to work with. From Solidity you send ether with a deployment like this: new Vault{value: 1 ether}().
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Vault {
address public owner;
constructor() payable {
owner = msg.sender;
console.log("Vault funded with", msg.value, "wei");
}
}
contract Main {
constructor() payable {
console.log("Main deployed with", msg.value / 1 ether, "ETH");
}
function main() external {
Vault vault = new Vault{value: 3 ether}();
console.log("vault balance:", address(vault).balance / 1 ether, "ETH");
console.log("Main balance :", address(this).balance / 1 ether, "ETH");
}
}Main deployed with 100 ETH
Vault funded with 3000000000000000000 wei
vault balance: 3 ETH
Main balance : 97 ETH
The first line comes from Main's own deployment, before main() runs.
Constructors and inheritance
When a contract inherits from another, the parent's constructor runs first. Arguments for the parent are passed in the inheritance list or with a modifier-like syntax on the constructor:
contract Named {
string public name;
constructor(string memory name_) { name = name_; }
}
contract Pet is Named {
constructor() Named("Rex") {} // pass the parent's argument
}You'll see this in detail in the Inheritance lesson.
Constructors are not for everything
- Keep constructors small: deployment gas grows with what they do.
- Values that never change after construction can be marked
immutable, which is much cheaper to read than storage (covered in immutable & constant). - Contracts behind a proxy (Upgradeable Contracts) cannot use constructors for their state; they use an
initializefunction instead.
Test yourself
How many times can a constructor run?
- Every time the contract is called
- Exactly once, during deployment
- Once per user
- Whenever the owner calls it
What happens if a constructor reverts?
- The contract is deployed with default values
- The contract is not deployed and the deployment fails
- Only the state set before the revert is kept
- The constructor is retried
How do you deploy Vault with 1 ether from another contract?
new Vault(1 ether)new Vault{value: 1 ether}()Vault.deploy{value: 1}()payable(new Vault())