Using Constants and Immutables in Solidity
📖 Scenario: You are creating a simple smart contract for a blockchain-based voting system. You want to set some fixed values that never change after deployment, such as the election name and the maximum number of votes per person.
🎯 Goal: Build a Solidity contract that uses constant and immutable variables to store fixed data and demonstrate how to read them.
📋 What You'll Learn
Create a
constant string variable called ELECTION_NAME with the value "Blockchain Election 2024".Create an
immutable unsigned integer variable called maxVotesPerPerson set via the constructor with the value 3.Write a public function
getElectionInfo that returns both ELECTION_NAME and maxVotesPerPerson.Deploy the contract and print the election info.
💡 Why This Matters
🌍 Real World
Constants and immutables help save gas and ensure important values do not change after deployment in blockchain smart contracts.
💼 Career
Understanding these keywords is essential for blockchain developers to write efficient and secure smart contracts.
Progress0 / 4 steps