0
0
Blockchain / Solidityprogramming~10 mins

Constants and immutables in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constants and immutables
Declare constant or immutable
Assign value once
Use value in contract
Attempt to change value?
YesError: Cannot modify
No
Execution continues
This flow shows how constants and immutables are declared once, assigned a value, used, and cannot be changed later.
Execution Sample
Blockchain / Solidity
contract Example {
  uint256 constant MAX_SUPPLY = 1000;
  uint256 immutable creationTime;

  constructor() {
    creationTime = block.timestamp;
  }
}
This contract sets a constant max supply and an immutable creation time set once at deployment.
Execution Table
StepActionVariableValue AssignedResult/Note
1Declare constant MAX_SUPPLYMAX_SUPPLY1000Value fixed at compile time
2Declare immutable creationTimecreationTimeunassignedWill assign in constructor
3Constructor runscreationTimeblock.timestamp (e.g. 1686000000)Value set once at deployment
4Use MAX_SUPPLY in functionMAX_SUPPLY1000Read-only value used
5Use creationTime in functioncreationTime1686000000Read-only value used
6Attempt to modify MAX_SUPPLYMAX_SUPPLYErrorCannot modify constant
7Attempt to modify creationTimecreationTimeErrorCannot modify immutable
8Contract execution continues--Constants and immutables remain unchanged
💡 Execution stops on errors if modification of constant or immutable is attempted
Variable Tracker
VariableStartAfter DeclarationAfter ConstructorFinal
MAX_SUPPLYundefined100010001000
creationTimeundefinedunassigned16860000001686000000
Key Moments - 3 Insights
Why can't we change the value of MAX_SUPPLY after declaring it?
Because MAX_SUPPLY is declared as a constant, its value is fixed at compile time and cannot be changed later, as shown in execution_table step 6 where modification causes an error.
When is the immutable variable creationTime assigned a value?
It is assigned once during the constructor execution (step 3 in execution_table), and after that it cannot be changed.
What happens if we try to modify an immutable variable after deployment?
An error occurs and the contract execution stops or reverts, as shown in step 7 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does creationTime have after the constructor runs?
Aunassigned
B1000
Cblock.timestamp (e.g. 1686000000)
D0
💡 Hint
Check execution_table row 3 where creationTime is assigned during constructor
At which step does the contract throw an error when trying to modify a constant?
AStep 6
BStep 4
CStep 2
DStep 8
💡 Hint
Look at execution_table row 6 where modifying MAX_SUPPLY causes an error
If we remove the assignment of creationTime in the constructor, what will happen?
AcreationTime is assigned automatically to block.timestamp
BcreationTime remains unassigned and causes an error on deployment
CcreationTime gets default value 0 and can be changed later
DContract compiles and runs normally
💡 Hint
Immutable variables must be assigned once; see variable_tracker and execution_table step 3
Concept Snapshot
Constants and immutables in blockchain contracts:
- Constants: fixed at compile time, cannot change
- Immutables: assigned once at deployment (constructor), then fixed
- Both save gas and improve security
- Attempting to modify causes errors
- Use constants for fixed values, immutables for deployment-time values
Full Transcript
This visual execution trace shows how constants and immutables work in blockchain contracts. Constants like MAX_SUPPLY are set once at compile time and cannot be changed. Immutables like creationTime are assigned once during contract deployment in the constructor and then remain fixed. The execution table walks through declaring these variables, assigning values, using them, and what happens if you try to modify them (errors occur). The variable tracker shows how values change from start to final. Key moments clarify common confusions about when and why these values cannot be changed. The quiz tests understanding by referencing specific steps in the execution. This helps beginners see clearly how constants and immutables behave step-by-step.