What is the difference between the constant and immutable keywords in Solidity and how do I use them?
Solidity Keywords – Difference Between Constant and Immutable in Solidity
constantimmutablekeywordsoliditysolidity-0.6.x
constantimmutablekeywordsoliditysolidity-0.6.x
What is the difference between the constant and immutable keywords in Solidity and how do I use them?
Best Answer
Both
immutable
andconstant
are keywords that can be used on state variables to restrict modifications to their state. The difference is thatconstant
variables can never be changed after compilation, whileimmutable
variables can be set within the constructor.From the docs:
An example code snippet is as follows:
It is important to note that the compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value.