Contract A {
    uint public x = 1;
    uint private y = 2;

    function getY() external view returns(uint){
        return y;

If a contract has important global variables that need to be queried by upkeep bots, other contracts, and frontends, what is best practice for making the global variable public (with it's own getter created by the compiler) vs private and making a getter function in the contract?

I've read the solidity documentation, Mastering Ethereum, and other resources for when to make a global variable public vs private but all I've found is discussions of when to make a function private or public. It's not clear to me the difference other than needing to make a getter function for private variables.


2 Answers 2


Private variables do not have a getter function. By default, a function is private.

Only public variables get a getter function from the compiler.

Hope that helps.


I would say the logic is quite similar as with function visibility, with the obvious exception that public variables generate getter functions.

I personally like to make most variables public for the following reasons:

  1. Easier for others to understand how the contract works if they can just query its data directly

  2. Don't give the impression that some data is secret by making it private. All data is public, either explicitly or implicitly

  3. Helps in all sorts of debugging

What I keep private is some internal things which wouldn't help anyone anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.