Back
Lesson 4:
Immutable
Introduction to immutable in Solidity
Visit desktop version for better experiences.
Immutable
Variables declared as immutable
are a less restricted than those declared as constant
. Immutable variables can be assigned a value at construction time. The value can be changed at any time before deployment and then it becomes permanent.
One additional restriction is that immutables can only be assigned to inside expressions for which there is no possibility of being executed after creation. This excludes all modifier definitions and functions other than constructors.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Immutable {
// coding convention to uppercase constant variables address public immutable MY_ADDRESS;
uint256 public immutable MY_UINT;
constructor(uint256 _myUint) {
MY_ADDRESS = msg.sender;
MY_UINT = _myUint;
}
}