Solidity Visibility Modifiers

Visibility modifiers define the visibility for both state variables and functions. Visibility modifiers determine whether or not you are able to access a variable or execute a function.

Visibility Modifers

There are four visibility modifiers available in the Solidity programming language. They are:

  • public
  • private
  • internal
  • external

Public

Public functions and variables can be accessed by all parties within and outside the contract. When the visibility is not specified, the default visibility of a function is public.

If a variable is has public visibility, the Solidity compiler automatically creates a getter function for accessing the value of the variable.

If you want a variable or function to be accessed from within the contract or from outside the contract, use public visibility.

Private

Private functions and variables are only accessible within the contract in which they were declared. Private functions are also the only functions that cannot be inherited by other functions.

You would think that if you mark a variable or function as private then it would not be visible on the blockchain. This is false. The variable and function are visible to observers of the blockchain. You are not able to access them from other contracts.

Private is the most restrictive of the four visibility modifiers.

Internal

Internal functions and variables are only accessible within the contract in which they were declared. Internal functions and variable can be accessed from derived contracts. They cannot be accessed from outside the contract.

Internal is the default visibility for state variables.

External

Only functions can have the external visibility modifier. External functions can only be called from outside the contract in which they were declared. They can't be called internally.

Did you find this article valuable?

Support All Things Tech by becoming a sponsor. Any amount is appreciated!