Why are state variables private rather than public in OpenZeppelin Contracts?

I have seen in tokens that use the variables decimals and etc as private and create a function to access them, why?

1 Like

You can find some explanation of visibility in this documentation: https://solidity.readthedocs.io/en/v0.7.4/contracts.html#visibility-and-getters, as for why create a function to access private variables, maybe in order to avoid exposing variables directly, and I think we should ask @abcoathup for a real reason.

1 Like

Hi @franco_villa_santana,

(Thanks @Skyge as always for being awesome :pray:)

State variables being private is part of the OpenZeppelin Contracts style guidelines:
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/GUIDELINES.md#solidity-code

The explanation for this can be found in the Release notes:

_From: https://github.com/OpenZeppelin/openzeppelin-contracts/releases/tag/v2.0.0_

All state variables are now private , which means that derived contracts cannot access them directly, but have to use getters. This is to increase encapsulation, to be able to reason better about the code.

1 Like

I have a question relating to this, am new to solidity but i have an implementation using ERC721EnumerableUpgradeable.

contract ERC721 is ERC721EnumerableUpgradeable {

}

I receive undeclared value or function when I try using any of the private variables or functions declared in ERC721EnumerableUpgradeable.

Please how do I use the private variables or methods declared in ERC721EnumerableUpgradeable from my new derived contract ERC721