Initialization of State Variable

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:

1. Initialization at the Time of the Declaration

Below is the Solidity program to initialize the state variable at the time of declaration:

Solidity




// Solidity program to initialize 
// state variable at the time of declaration
// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
  
contract w3wikiStateDemo {
    
  string public name = "Geek Adwitiya";
  uint public age = 23;
}


Output: 

 

Explanation: A simple contract is created, to demonstrate 1st way in which one can initialize state variables ‘name’ and ‘age’.

Note: Adding public as access modifier between the variable name and the data type, allows solidity to create a get() method automatically.

2. Initialization Using Constructors

Below is the Solidity program to initialize state variables using constructors:

Solidity




// Solidity program to initialize 
// state variables using constructors
// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
  
contract w3wikiStateDemo {
    string public name;
    uint public age;
    
      Constructor() public
    {
      name = "Geek Adwitiya";
      age = 23;
    }
}


Output:

 

Explanation: In the above code, we’ve created a contract and declared two state variables as – ‘name’ and ‘age’. And initialize them by making use of a ‘Constructor’. This is another way to initialize a state variable.

3. Initialization Using Setter Method

Below is the Solidity program to initialize state variables using the setter method:

Solidity




// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
  
contract w3wikiStateDemo {
    string public name;
    uint public age;
        
      function setName() public
    {
      name = "Geek Adwitiya";
    }
    
      function setAge() public
    {
      age = 22;
    }
}


Output:

 

Explanation: This is the 3rd way of initializing a state variable by making use of the setter function, set(). We’ve created two separate functions to initialize each of the state variables.

Above mentioned 3 ways are the only way to initialize a state variable, we cannot initialize state variables after declaration. Either do it while declaring or make use of a constructor or set() function. 

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:...