Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Constants and immutables
Declare constant or immutable
↓
Assign value once
↓
Use value in contract
↓
Attempt to change value?
Yes→Error: 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.

Practice

(1/5)
1. What is the main difference between constant and immutable variables in a blockchain smart contract?
easy
A. Both constant and immutable variables can be changed after deployment.
B. constant variables can be changed anytime, immutable variables cannot be changed.
C. immutable variables are fixed at compile time, constant variables are set during contract creation.
D. constant variables are fixed at compile time, immutable variables are set once during contract creation.

Solution

  1. Step 1: Understand constant variables

    constant variables are assigned a value at compile time and cannot be changed later.
  2. Step 2: Understand immutable variables

    immutable variables are assigned once during contract creation (deployment) and then remain fixed.
  3. Final Answer:

    constant fixed at compile time, immutable set once at creation -> Option D
  4. Quick Check:

    constant = compile time, immutable = creation time [OK]
Hint: Constant fixed at compile, immutable set once at deployment [OK]
Common Mistakes:
  • Thinking immutable can be changed after deployment
  • Confusing when constants are assigned
  • Assuming both can be modified anytime
2. Which of the following is the correct way to declare a constant integer named MAX_SUPPLY with value 1000 in Solidity?
easy
A. constant uint MAX_SUPPLY = 1000;
B. uint constant MAX_SUPPLY = 1000;
C. uint MAX_SUPPLY constant = 1000;
D. constant MAX_SUPPLY uint = 1000;

Solution

  1. Step 1: Recall Solidity constant syntax

    In Solidity, the type comes first, then the keyword constant, then the variable name.
  2. Step 2: Check each option

    uint constant MAX_SUPPLY = 1000; matches the correct syntax: uint constant MAX_SUPPLY = 1000;. Others have wrong order.
  3. Final Answer:

    uint constant MAX_SUPPLY = 1000; -> Option B
  4. Quick Check:

    Type + constant + name = correct order [OK]
Hint: Type first, then constant keyword, then variable name [OK]
Common Mistakes:
  • Placing constant before type
  • Putting constant after variable name
  • Missing semicolon at end
3. Consider the Solidity contract snippet:
contract Example {
    uint public immutable creationTime;
    constructor() {
        creationTime = block.timestamp;
    }
}

What will creationTime hold after deployment?
medium
A. The timestamp when the contract was deployed
B. The timestamp when the contract was compiled
C. Zero, because it is not initialized
D. It will cause a compilation error

Solution

  1. Step 1: Understand immutable assignment

    immutable variables can be assigned once during contract creation, here in the constructor.
  2. Step 2: Analyze the assignment

    creationTime = block.timestamp; sets it to the current block timestamp at deployment.
  3. Final Answer:

    The timestamp when the contract was deployed -> Option A
  4. Quick Check:

    Immutable set at deployment time [OK]
Hint: Immutable set once in constructor at deployment [OK]
Common Mistakes:
  • Thinking it holds compile time value
  • Assuming default zero value
  • Confusing immutable with constant
4. What is wrong with this Solidity code snippet?
contract Test {
    uint public immutable value;
    function setValue(uint _val) public {
        value = _val;
    }
}
medium
A. You cannot assign to an immutable variable outside the constructor.
B. The variable value should be declared as constant.
C. The function setValue should be marked as view.
D. There is no error; the code is correct.

Solution

  1. Step 1: Recall rules for immutable variables

    immutable variables can only be assigned once during contract creation, typically in the constructor.
  2. Step 2: Check assignment location

    Here, value is assigned inside a public function, which is not allowed.
  3. Final Answer:

    Cannot assign immutable outside constructor -> Option A
  4. Quick Check:

    Immutable assigned only once in constructor [OK]
Hint: Immutable only set in constructor, not in other functions [OK]
Common Mistakes:
  • Trying to assign immutable in regular functions
  • Confusing constant and immutable rules
  • Thinking public functions can set immutable
5. You want to store a fixed token symbol in your smart contract that never changes and a deployment timestamp that is set once at deployment. Which declarations are correct?
contract Token {
    // Declare symbol
    // Declare deploymentTime
    constructor() {
        deploymentTime = block.timestamp;
    }
}
hard
A. string public constant symbol; uint public immutable deploymentTime = block.timestamp;
B. string public immutable symbol = "TKN"; uint public constant deploymentTime = block.timestamp;
C. string public constant symbol = "TKN"; uint public immutable deploymentTime;
D. string public immutable symbol = "TKN"; uint public immutable deploymentTime = block.timestamp;

Solution

  1. Step 1: Declare fixed symbol as constant

    Since symbol never changes and is known at compile time, declare as constant with value assigned inline.
  2. Step 2: Declare deploymentTime as immutable

    Deployment timestamp is set once at deployment, so declare as immutable and assign in constructor.
  3. Final Answer:

    symbol constant with value, deploymentTime immutable assigned in constructor -> Option C
  4. Quick Check:

    Constant fixed value, immutable set once at deployment [OK]
Hint: Constant for fixed value, immutable for deployment-time set [OK]
Common Mistakes:
  • Assigning immutable variables inline without constructor
  • Declaring constant without value
  • Confusing when to use constant vs immutable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard