Variable Scopes and Visibility Modifiers
Get to know the different scopes and levels of visibility for variables in Solidity.
In terms of scope, Solidity supports the following three variable types:
-
State variables: Variables whose values are permanently stored in a contract’s storage. We can refer to them anywhere in the contract in which they are declared, and depending on their visibility in other contracts.
-
Local variables: Variables whose values are only stored during the execution of a function and can only be referred to within that function.
-
Globally available variables: Predefined variables that provide information about the blockchain. They can be referred to in any Solidity contract without having to declare them.
State variables
State variables have to be declared within the curly brackets following a contract's header but outside any function. They can be of any primitive or complex type.
For instance, in the following contract, we declare a global variable n
of uint
type.
pragma solidity >=0.5.0 <0.9.0;contract exampleStateVariable {uint256 n;}
The value of n
would be ...