Solidity Guide – Understanding Private Variables in Solidity

privatesolidityvisibility

The following variables here, assuming solidity 4.24 or later.

mapping(something => something) private yourmapping;

bool private yourbool;

address private youraddress;

uint256 private yournumber;

function dosomething() private {
*do stuff*
}

I see quite a few questions about hiding stuff in solidity. I'm not interested in that. My understanding of private variables is that they do not allow any other contract to call or change those variables, meaning that only the scope within the current contract is allowed to call or modify those variables. Am I wrong?

Further, any links or other information regarding private variables would be highly useful. I have added a private function as well for bonus points. What variables are allowed to be private? What other items can be considered private?

Best Answer

According to Solidity Documentation:

Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.

In Sol, the private keyword doesn't really mean it is private to the contract consumers. What it means is for example that: a contract B imports contract A and contract A has a private variable V. From contract B, you cant change variable V.

contract A {

    string private V;

}

contract B is A{
    ...
    can't change A.V
    ...
}

hope it helps

[EDIT] - Typo

Related Topic