Solidity theorytheory 0/50 · 0%
Foundations · easy

6. Constructor and immutability

constructor, immutable and constant.

The `constructor` runs exactly once, during deployment, and is not part of the ABI afterwards. It is where you set the owner, the token address and other deploy-time configuration.

`constant` values are fixed at compile time; `immutable` values are set once in the constructor and then baked into the bytecode. Both are far cheaper to read than storage.

uint256 public constant RATE = 5;
address public immutable token;

constructor(address token_) {
    token = token_;
}

Check your understanding

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

  2. 2. When is an `immutable` assigned?

  3. 3. Why prefer `constant`/`immutable` over storage?