On day 3 of my learning Patrick Collins course “Learn Blockchain, solidity and full stack web3 development with javascript, " the Solidity Visibility State/Modifiers state was the focus.
This is my note.
Solidity is a programming language to develop smart contracts on the Ethereum blockchain.
One of the critical features of Solidity is its support for different visibility or state modifiers, which determine the accessibility of variables and functions within a contract.
In today's class, we will discuss the four main visibility or state modifiers in Solidity: public, private, external, and internal and provide a simple code example for each.
Public
Any contract or external account can access variables and functions that are declared public.
This means that they can be read and written from outside the contract. Public variables and functions are the default visibility if no other modifier is specified on a contract.
pragma solidity ^0.8.0;
contract Example {
uint public value;
function setValue(uint _value) public {
value = _value;
}
}
In this example, the value variable is public and can be accessed by any external account.
The setValue() function is also public and allows any external account to change the value
of the value variable.
If this contract is deployed, anyone can set the value of the _value to any new number as it is public.
Private
Variables and functions declared private can only be accessed within the contract in which they are defined.
This means that they can only be read and written from within the contract and cannot be accessed outside the contract.
pragma solidity ^0.8.0;
contract Example {
uint private value;
function setValue(uint _value) private {
value = _value;
}
function getValue() private view returns (uint) {
return value;
}
}
In this example, the value variable is private and can only be accessed within the contract.
The setValue() function is also private, which means it can only be called within the contract and not by any external account.
The getValue() function is also private, meaning it can only be called within the contract and not by any external account.
It means another function can call the value argument, but it must be within the contract.
External
Functions declared as external can only be called from outside the contract. These functions are also not accessible from within the contract.
This allows a contract to have a specific interface exposed to the outside world.
pragma solidity ^0.8.0;
contract Example {
function externalFunction() external {
// function code here
}
}
In this example, the externalFunction() can only be called from outside the contract and is not accessible from within the contract.
Internal
Functions and variables declared as internal can be accessed only within the contract but also in its derived contracts.
This means that internal functions can be called, and internal variables can be read or written by any contract that inherits from the contract in which they are defined.
pragma solidity ^0.8.0;
contract Example {
function internalFunction() internal {
// function code here
}
}
In this example, the internalFunction() can be called within the contract and in any contract that inherits from it.
In conclusion, Solidity’s state modifiers provide a way to control the accessibility of variables and functions within a contract, which is crucial for maintaining the integrity and security of smart contracts on the Ethereum blockchain.
Understanding the different state modifiers and how to use them correctly is essential to developing smart contracts in Solidity.
Top comments (0)