Visibility – public, private, external, internal

Published by Mario Oettler on

In some cases, it is helpful to restrict access to variables and functions. This is done with one of the four visibility keywords public, private, external, and internal. Those keywords can be used for variables and functions. However, in some cases they behave a bit differently.

public

If a variable is set to public, the compiler automatically creates a getter-function. This function is described in the ABI so that user interfaces can easily access the content of the variable. Public also means that the owner contract, EOAs, and other contracts have read access to the variable.

If a function is set to public, EOAs, other contracts, derived contracts, and the owner contract can invoke it. They are part of the contract interface.

private

Private state variables and functions are only visible and accessible by the owner contract. Other contracts, derived contracts, and EOAs have no access.

Internal

Internal state variables and functions are visible and accessible by the owner contract and derived contracts. Other contracts and EOAs have no right to access them.

External

Only functions can have the visibility keyword external. External functions are accessible from other contracts and EOAs. They are part of the contract interface.

Note

Everything stored in a smart contract is visible to all observers external to a blockchain. Using the private or internal keyword only restricts other contracts from reading information or invoking functions. But they are not sufficient to keep things secret.

Categories:

if()