Original title: "Introduction to Solidity Minimalist: 9. Constants constant and immutable"
Original author: 0xAA
All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity
Here In the first lecture, we introduce two keywords in solidity, constant (constant) and immutable (invariant). After the state variable declares these two keywords, the value cannot be changed after the contract; and it can also save gas. Also, only numeric variables can be declared constant and immutable; String and bytes can be declared constant, but not immutable.
< /p>
Variables must be initialized when they are declared and cannot be changed afterwards. If you try to change it, the compilation will fail.

Immutable variables can be initialized at declaration time or in the constructor, so they are more flexible.

You can use global variables such as address(this), block.number , or custom functions to initialize immutable variables. In the following example, we use the test() function to initialize IMMUTABLE_TEST to 9:

In this lecture, we introduce two keywords in solidity, constant (constant) and immutable (invariant), so that variables that should not change remain unchanged.
Original link
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia

