State Variables
Published Last updated
Key Takeaway
Permanent data stored directly in a smart contract's blockchain storage, maintaining values persistently across function calls and transactions, representing the contract's long-term memory and state.
Learn These First
What Is State Variables?
Permanent data stored directly in a smart contract's blockchain storage, maintaining values persistently across function calls and transactions, representing the contract's long-term memory and state.
How State Variables Works
Frequently Asked Questions
What happens to state variables if the smart contract is deleted or upgraded?
State variables stored on the blockchain remain permanently even if you could 'delete' a contract, though true deletion is rare. Most contracts are immutable—their code and state variables persist indefinitely unless the contract includes selfdestruct functionality (increasingly discouraged). For upgradeable contracts using proxy patterns, state variables typically persist in proxy contract storage while implementation logic changes. New implementation contracts must maintain compatible storage layouts to avoid corrupting existing state variables. When upgrading, developers must ensure new code interprets storage slots identically to old code. Poorly designed upgrades can make state variables inaccessible or misinterpreted. Some contracts intentionally include migration functions copying state variables to new contracts during major upgrades. Understanding upgrade mechanisms helps users evaluate risks of trusting upgradeable contracts with long-term value storage.
Why do some smart contract operations cost much more gas than others?
Gas costs correlate strongly with state variable operations—writing to permanent blockchain storage is significantly more expensive than temporary calculations. When you approve token spending, mint NFTs, or deposit in DeFi, you're modifying state variables that all network nodes must record permanently, costing substantial gas. Simple calculations using temporary variables or reading existing state variables costs much less. Complex operations like token swaps might read multiple state variables, perform calculations, then write updated state variables, accumulating costs. Contract interactions calling other contracts multiply gas costs as each contract executes its logic and potential state modifications. Understanding this helps users anticipate costs: operations merely reading data cost little, while operations creating or modifying permanent records cost significantly more due to blockchain's permanent storage requirements and distributed validation.
Can anyone see what's stored in a smart contract's private state variables?
Yes, anyone can see all state variable values regardless of visibility modifiers by examining blockchain storage directly. 'Private' in Solidity means other contracts cannot programmatically access those variables, not that the data is secret. Blockchain data is inherently transparent—every node stores complete blockchain state including all contract storage. Tools allow anyone to query contract storage slots and read variable values even marked private. This transparency is fundamental to blockchain's trustless verification properties. If you need truly private data, it must be encrypted before storing on-chain or kept off-chain entirely. Visibility modifiers control programmatic access between contracts but don't provide privacy from blockchain observers. Understanding this prevents dangerous assumptions about data confidentiality in smart contracts—never store sensitive unencrypted information expecting privacy based on variable visibility.
Common Misconceptions About State Variables
State variables marked 'private' in Solidity contracts are secret and cannot be read by outsiders.
Private state variables are NOT secret—blockchain storage is inherently transparent and anyone can read all state variable values by examining blockchain data directly. 'Private' in Solidity means other smart contracts cannot programmatically access those variables through standard function calls, not that the data is hidden from blockchain observers. Every node stores complete blockchain state including all contract storage slots, and tools allow anyone to query these storage locations regardless of visibility modifiers. This transparency is fundamental to blockchain's trustless properties—observers can verify contract behavior and storage without permission. If you need actual privacy, data must be encrypted before storing on-chain or kept off-chain entirely. Never assume state variables provide confidentiality based on visibility keywords—all blockchain data is ultimately public.
State variables automatically save their values—I don't need to explicitly update them in transactions.
State variables save values only when explicitly modified through transactions that change blockchain state. If a function calculates a new value but doesn't assign it to the state variable through a transaction, the calculation is discarded and the original value persists. State variables don't auto-update based on time, external events, or passive calculations—they change only through transaction execution calling functions that modify storage. This requires understanding the difference between reading state (free or low-cost queries that don't change anything) and writing state (transactions that cost gas and permanently modify storage). Many users incorrectly assume contracts automatically update values periodically, but blockchain is deterministic and state changes only through explicit transaction execution triggering storage modifications. Contracts requiring time-based updates need external transaction triggers.
Using more state variables is always better than complex calculations because storage is permanent.
Excessive state variable usage is generally bad practice due to high gas costs and storage constraints. While state variables provide permanent storage, writing to them is the most expensive blockchain operation. Good contract design minimizes state variable reads/writes, using temporary memory or calldata for intermediate calculations and storing only essential permanent data. Over-reliance on state variables makes contracts prohibitively expensive to use as gas costs accumulate. Additionally, blockchain storage isn't unlimited—contracts with unbounded state variable growth (large arrays or mappings) can become too expensive to interact with as they accumulate data. Optimal design balances permanent storage for necessary data with efficient temporary calculations for intermediate values. Sometimes complex calculations using memory are cheaper than multiple state variable operations, even though calculations disappear after execution while storage persists.