Solidity theorytheory 0/50 · 0%
Structure · medium

21. Constructors and immutability

Setting values once at deploy time.

A `constructor` runs exactly once, during deployment, and is not part of the deployed bytecode's callable surface. Values that never change afterwards should be `immutable` (set in the constructor, stored in code) or `constant` (fixed at compile time). Both are far cheaper to read than storage.

address public immutable owner;
uint256 public constant FEE_BPS = 250;

constructor() { owner = msg.sender; }

Never assume the deployer is the intended owner on a factory deployment — pass the owner explicitly when a contract is created by another contract.

Check your understanding

  1. 1. How many times does a constructor run?

  2. 2. Where is an `immutable` value stored?

  3. 3. Which must be known at compile time?