theucheedeoga

theucheedeoga

Variable Visibility in Solidity.

In Solidity, we can always control how our Variables can be accessed. In this case, whenever we create any variable we can assign some keywords to these variables that limit access to these variables.

There are three keywords in Solidity that show the visibility of a Variable. They are:

  • Private.

  • Internal.

  • Public.

A "Private" Variable means that the variable can only be read from that very particular smart contract. If you try to read that variable from outside the smart contract it is going to fail. But this is only true in this context because it is in the ethereum virtual machine. When it is stored in the public Ethereum Blockchain, it can actually be read by anyone.

Example

Pragma solidity ^0.8.0;

contract variableVisibility{ uint private a;

};

We have just created a variable with a datatype of uint and gave it the name "a". We also have made the variable to have a visibility of "private". Remember we said a private variable can only be read from the same smart contract. Let us be more specific and create a function and use the variable we have just created.

Pragma solidity ^0.8.0;

contract variableVisibility{ uint private a;

function sum() external{ uint b = a + 1;

  • }*

}

So inside the sum function, we can be able to read the variable "a" within the smart contract.

  • Internal Variable: These variables are can be red from inside the smart contract and other smart contracts that can be inherited from it. This also means that you cannot read this variable from outside the smart contract.

Example

Pragma solidity ^0.8.0;

contract variableVisibility{ uint internal a;

};

  • Public Variable: With the public keyword it means that you can be able to read this variable not only within the smart contract but also from other smart contracts that inherits from it and also from outside the smart contract.

Example

Pragma solidity ^0.8.0;

contract variableVisibility{ uint public a;

};

When we create a variable with public visibility, what solidity does behind the hood is that it creates a public function with the same name as the variable that returns the variable datatype.

function a()public view returns(uint){ return a; };

So if we declare this same function in the smart contract solidity will throw an error to say that it has already been declared. Also, it is important to note here that solidity automatically recognizes a variable to be a private variable when non of the variable visibilities are mentioned. So

Pragma solidity ^0.8.0;

contract variableVisibility{ uint a; };

This means that the variable named a with the datatype of uint is a private variable. Solidity automatically recognizes it as a private variable.

There is another kind of variable called the built-in Variable that comes with solidity. We will cover that in the next tutorial. You can follow me on Twitter on my Journey to becoming a Blockchain developer. where I share articles and stuffs related to blockchain and cryptocurrency.

#solidity#variables#blockchain#smart-contracts#beginners#beginner
 
Share this