State variables store information permanently on the blockchain. They keep data safe and shared for everyone using the program.
State variables in Blockchain / Solidity
type variableName;
// Example:
uint256 myNumber;State variables are declared inside a contract but outside any function.
They are stored permanently on the blockchain and cost gas to change.
count that stores a number.contract Example {
uint256 count;
}name that stores text.contract Example {
string name;
}isActive that stores true or false.contract Example {
bool isActive;
}This contract has a state variable storedNumber that saves a number. The setNumber function changes it, and getNumber returns it.
pragma solidity ^0.8.0; contract SimpleStorage { uint256 storedNumber; function setNumber(uint256 _number) public { storedNumber = _number; } function getNumber() public view returns (uint256) { return storedNumber; } }
Changing state variables costs gas because it updates the blockchain.
Reading state variables (view functions) does not cost gas if called externally.
State variables keep their value between function calls and transactions.
State variables store data permanently on the blockchain.
They are declared inside contracts but outside functions.
Use them to remember important information for your blockchain app.
Practice
state variable in a blockchain smart contract?Solution
Step 1: Understand what state variables do
State variables store data permanently on the blockchain, unlike local variables which are temporary.Step 2: Compare options with definition
Only A variable that stores data permanently on the blockchain describes permanent storage on the blockchain, which matches the definition of state variables.Final Answer:
A variable that stores data permanently on the blockchain -> Option AQuick Check:
State variable = permanent blockchain storage [OK]
- Confusing state variables with local variables
- Thinking state variables reset after function ends
- Assuming state variables store data off-chain
Solution
Step 1: Recall Solidity syntax for state variables
State variables are declared outside functions with a type and visibility, e.g.,uint256 public count;.Step 2: Check each option
uint256 public count; matches correct syntax. Options A, B, and C use incorrect keywords or syntax not valid for state variables.Final Answer:
uint256 public count; -> Option CQuick Check:
State variable declaration = type + visibility + name [OK]
- Using function keyword to declare variables
- Using var or let which are not Solidity keywords
- Declaring variables inside functions as state variables
contract Counter {
uint256 public count = 0;
function increment() public {
count += 1;
}
}What will be the value of
count after calling increment() three times?Solution
Step 1: Understand initial value and increment
The state variablecountstarts at 0 and increases by 1 each timeincrement()is called.Step 2: Calculate after three calls
After three calls, count = 0 + 1 + 1 + 1 = 3.Final Answer:
3 -> Option BQuick Check:
0 + 3 increments = 3 [OK]
- Forgetting count starts at 0
- Assuming count resets after each call
- Thinking count is private and inaccessible
contract Example {
uint256 count;
function setCount(uint256 _count) public {
uint256 count = _count;
}
}Solution
Step 1: Analyze variable declarations
The state variablecountis declared correctly. InsidesetCount, a new local variablecountis declared, which hides the state variable.Step 2: Understand shadowing effect
Assigning to localcountdoes not change the state variable. This is a common mistake called variable shadowing.Final Answer:
Local variable shadows state variable inside function -> Option AQuick Check:
Local variable shadows state variable = bug [OK]
- Thinking state variable is missing
- Ignoring variable shadowing
- Assuming function needs return type
Solution
Step 1: Understand state variable declaration for permanent storage
State variables must be declared outside functions with proper type and visibility, e.g.,string public name;.Step 2: Update state variable in a public function
To change the stored name, write a public function that assigns a new value to the state variable.Final Answer:
Declare string public name; as state variable and update it in a public function -> Option DQuick Check:
State variable + public update function = correct pattern [OK]
- Declaring variables inside functions only
- Using memory keyword for state variables
- Not providing visibility for state variables
Ready to prove it?
Pass the test and this pattern moves unknown
