Reading and Writing to a State Variable

1. Reading From a State Variable 

The values in a state variable can be read easily by the use of functions, just add a ‘view’ keyword to the signature of the function. Below is the Solidity program to read from a state variable:

Solidity




// Solidity program to read from 
// a state variable:
// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
  
contract ReadStateVariable {
    
  string public message = "Hello Adwitiya Mourya!"
  uint private num = 10;
      
  //* Function to read the value of a state variable  
  function get() public view returns (uint) 
  {
    return num;
  }
}


Explanation: In solidity for every public state variable, a get() is automatically created to read its value, hence I’ve created a get() only for num because it’s declared as private. 

You can execute the code on the online Remix IDE or the one you’ve installed on your local computer. Just open it and type the above code, click Deploy > then your output should be something like this when you call the get() and message():

Output:

 

2. Writing to a State Variable 

When writing a value to a state variable, we need to send a transaction to do so. Below is the Solidity program to write to a state variable:

Solidity




// Solidity program to write to 
// a state variable
// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
  
contract w3wikiStateDemo {
    
  string public name;
  uint256 public num;
  
  //* Forces us to Set(Write) the values before 
  // deploying the contract
  constructor(string memory _name, uint _no) 
  {
    name = _name;
    num = _no;
  }
    
  //* A function to update(Write) the values stored 
  // in the state variables
  function update(string memory _name, uint _no)  public 
  {
    name = _name;
    num = _no;
  }
}


Output:

 

In the above output, we can see every time we need to write(update) the value of the state variable, we’ll have to send a transaction. 

Solidity – State Variables

Any variable created at the contract level is called the state variable (variables that aren’t inside a function) and it is stored permanently on the blockchain i.e., they are persistent and cannot be deleted. So, if you were to create a state variable in your contract and assign it some value, and came back after some time then its value would still be the same.  An important thing to note here is, you’ll have to pay some level of gas for every state variable declared. Hence, you should be careful every time you create a state variable. 

Note: Gas is a fee that is required to execute each transaction. Just like we need fuel to run a vehicle, we also need gas to execute the transactions on the blockchain. And the price of gas is calculated by the supply and the demand of miners in the network.

Important Points:

  • State variables directly store data on the blockchain that is, on the contract storage itself. So, if you were to store data in the state variable and come back after 1-week then your data would still be there.
  • State variables are declared inside a contract and outside the function.
  • Solidity automatically creates a get method with the same name for state variables.
  • The state variable can be assigned one of the access modifiers: public, internal, or private.
  • Since storages are not allocated dynamically, we’ve to re-compile each time we declare a state variable.
  • State variables are expensive as they cost gas.
  • Every instance of a contract will contain only those state variables, which were present in the contract before compilation.

Example:

Solidity




// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
  
contract w3wiki {
    // Created inside the contract 
    // but outside a function
    string public message; 
    
    function setMessage() public {
        message = "Hello Geek !";
    }
}


Explanation: In the above code, we’ve created a state variable – ‘message’ and its value is set to “Hello Geek !” through the set() function. Since we’ve used the access modifier as public, the solidity will automatically create a public get() function to get the value of the message variable which allows everyone to read the value of the message variable even from outside the contract.

Similar Reads

Scope of State Variables

...

Initialization of State Variable

We can only control the scope of state variables but we can’t do the same with local and global variables. So every state variable has some level of scope associated with it i.e. to what extent it will be accessible. In solidity, we can define a scope by using the following access modifiers for state variables:...

Reading and Writing to a State Variable

...

State Variables vs Local Variables

Now let’s see how many ways we can initialize a state variable. There are three ways in which one can initialize a state variable:...