Using visibility modifiers efficiently
Functions and state variables can have visibility modifiers. There are four visibility modifiers in solidity. Functions can be specified as public
, private
, internal
, or external
. State variables support all visibility levels except external. In this recipe, you will learn about visibility modifiers and how to use them.
How to do it...
If you want the function to be accessible both externally and internally, it has to be marked as
public
. Public state variables will generate agetter
function automatically:
pragma solidity ^0.4.21; contract Visibility { // Public state variables generate an automatic getter uint public limit = 110021; // Accessible externally and internally function changeLimit(uint _newLimit) public { limit = _newLimit; } }
You can see the getter
methods generated from the ABI output of this contract:
[{ "constant":false, "inputs":[{ "name":"_newLimit", "type":"uint256" ...