Constructor
A constructor is a special function that runs exactly once: when your contract is first deployed to the blockchain. Think of it as the setup step that prepares your contract before anyone can use it.
Unlike regular functions, a constructor has no name and there can only be one per contract. Its job is to initialize state, like setting a starting score, assigning an owner, or configuring default values.
Try It Yourself
Complete each task below to see how constructors accept parameters and initialize state.
Make the constructor payable so the contract can receive funds at deployment.
Payable Constructor
Toggle the Payable switch on the
A common use case: deploying a treasury or vault contract that needs initial capital to operate.
Constructor Modifiers
The Modifiers field lets you attach guard functions that run during deployment. This is an advanced feature mainly used with proxy patterns, where a modifier like initializer ensures setup logic runs exactly once.
For most contracts you won’t need constructor modifiers, but they’re available when the architecture calls for them.
What You Learned
- Constructors accept parameters so the deployer can pass in initial values at deploy time
- Constructors initialize state by wiring parameter data into
setVariable nodes, setting your contract’s starting values - Payable constructors accept native token on deploy, letting your contract start with funds
constructor modifiers add guard checks during deployment (advanced, used in proxy patterns)
Now that you know how to set up a contract with a constructor, you have all the building blocks to create complete smart contracts.
How hard was this?