State Variable

State variables are declared at the contract level and represent the contract’s state on the blockchain. They are stored on the Ethereum network and are accessible within the entire contract.

Syntax:

<type> <variable_name>;

Below is the Solidity program to implement the State variable:

Solidity




// Solidity program to implement
// State variable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
  
contract StateVariableExample {
  uint public count = 0;
  function increment() public 
  {
    count += 1;
  }
}


Output: Calling the increment function will increase the value of the count by 1. The value of the count can be accessed using the count public getter function generated by Solidity.

 

Solidity – Variable Scope

Variable scope is an essential concept in Solidity, the programming language for Ethereum smart contracts. Solidity has various types of variables with different scopes, such as local, state, and global variables.

Similar Reads

Local Variable

Local variables are declared within a function and are only accessible within that function. Their scope is limited, and their lifetime ends when the function execution is completed....

State Variable

...

Global Variable

State variables are declared at the contract level and represent the contract’s state on the blockchain. They are stored on the Ethereum network and are accessible within the entire contract....

Variable Scope

...