Smart Contracts
Constructor
A special function that runs once when a contract is deployed, used to initialize state variables.
Last Updated
2026-03-19
Related Concepts
What is Constructor?
A constructor is a special function in a smart contract that runs only onceat the exact moment the contract is deployed. It is used to initialize the contract's starting state and settings.
How does Constructor work?
- When writing code (e.g., in Solidity), the developer defines the constructor function.
- This function can accept "arguments" like the name of a token or an owner's address.
- When the contract is sent to the blockchain, these arguments are provided by the deployer.
- The blockchain executes the constructor logic, sets the initial variables, and then discards the code.
- The constructor code is never stored in the final contract's "runtime bytecode" and cannot be called again.
Why does Constructor matter?
Constructors are essential for setting the "birth" parameters of a contract. They ensure that roles like the "Owner" are assigned immediately upon deployment, preventing unauthorized users from taking control of the contract later.
Key features of Constructor
- Runs exactly once at deployment
- Used for initial state setup
- Can receive external parameters
- Not callable after deployment
- Essential for security and role assignment
Examples of Constructor
- An
ERC-20contract using a constructor tomintthe initial token supply to the creator. - A multisig wallet using a constructor to set the list of authorized signers.
- A DeFi protocol setting the initial "Treasury" address during its launch.
