Smart Contracts
State Variable
A variable stored permanently on the blockchain as part of a smart contract's data.
Last Updated
2026-03-19
Related Concepts
What is State Variable?
A state variable is a piece of data that a smart contract stores permanently on the blockchain. Unlike local variables that exist only during a function call, state variables persist across every transaction and function invocation.
How does State Variable work?
- Variables are declared in the contract with explicit types (uint256, address, bool, etc.).
- Each variable is assigned a storage slot in the EVM sequentially.
- Writing to a state variable costs gas and records the change permanently on-chain.
- Reading is cheaper free for view functions called off-chain.
Why does State Variable matter?
State variables are what enable contracts to track balances, ownership, and logic across time. Without them, contracts could only perform stateless, one-off calculations.
Key features of State Variable
- Persistent across all transactions and calls
- Writing costs gas; reading is cheap
- Each contract has isolated storage inaccessible to others
- Careful layout is critical for proxy upgrades
Examples of State Variable
In an ERC-20 token, balances is a state variable mapping addresses to amounts. In a DAO, votingDelay and votingPeriod track governance timelines.
In a lending protocol, totalDeposited accumulates user deposits over time.
