Modifiers in Solidity: Public, Private, Internal, and External

MD Jamil Kashem Porosh
4 min readSep 29, 2023

--

Solidity is a powerful programming language specifically designed for developing smart contracts on the Ethereum blockchain. When writing smart contracts, it’s essential to control access to certain functions and variables to ensure the security and integrity of your decentralized applications (DApps). Solidity offers a set of access modifiers that allow you to specify who can interact with various parts of your contract code.

In this blog post, we’ll explore four important modifiers: public, private, internal, and external, with easy-to-understand examples.

Image by Pete Linforth from Pixabay

What Are Modifiers?

Modifiers in Solidity are special keywords used to control access to functions and state variables within a smart contract. They act as gatekeepers, determining who can call a particular function or access a variable.

01. public Modifier

The public modifier is like an open-door policy, allowing functions and state variables to be easily accessed from anywhere, including external contracts. It’s the most permissive modifier, ensuring that everyone can interact with and use the specified functions and variables without restrictions. It’s like a public park where everyone is welcome to enjoy and utilize the available amenities. Here’s a simple example:

pragma solidity ^0.8.0;

contract ExampleContract {
uint256 public myNumber = 42;

function getMyNumber() public view returns (uint256) {
return myNumber;
}
}

The myNumber variable is declared as public, making it accessible from anywhere, even external contracts. The getMyNumber function is also public.

2. private Modifier

The private modifier acts like a locked door, tightly controlling access to functions and state variables within the current contract. It ensures that these elements can only be accessed and manipulated by the contract itself and are not visible or available to any external entities. It’s like a private room in a house that is exclusively accessible to the residents and hidden from the outside world.Here’s an example:

pragma solidity ^0.8.0;

contract ExampleContract {
uint256 private secretNumber = 12345;

function revealSecret() public view returns (uint256) {
return secretNumber;
}
}

With the private modifier, the secretNumber variable can only be accessed within this contract. No external or derived contract can access it directly.

*Can only be accessed within the contract it’s declared.

3. internal Modifier

The internal modifier acts as a semi-open door, providing access to functions and state variables not only within the current contract but also to derived contracts. It ensures that these elements cannot be accessed from external sources, such as external contracts or users. It’s like a family gathering where certain information and resources are shared among family members but remain inaccessible to outsiders. This modifier is particularly useful when implementing inheritance in Solidity, as it allows derived contracts to access and utilize the internal elements of the parent contract. Here’s an example:

pragma solidity ^0.8.0;

contract BaseContract {
uint256 internal internalNumber = 99;
}

contract DerivedContract is BaseContract {
function getInternalNumber() public view returns (uint256) {
return internalNumber;
}
}

The internalNumber variable is marked internal in the base contract. This allows it to be accessed by functions in derived contracts, like getInternalNumber in DerivedContract, but not from external contracts.

*Can only be accessed within the same contract (file).

4. external Modifier

The external modifier is like a one-way street, allowing functions to be accessed solely from external contracts or transactions. Functions marked as external cannot be called internally within the contract itself. It’s a way to ensure that these functions are exclusively invoked from outside the contract, prohibiting any internal calls. It’s like a customer service desk that can only be accessed by external customers, preventing employees from making use of those services internally. The external modifier is primarily used when you want to define functions that are specifically designed for external contract interactions. Here’s an example:

pragma solidity ^0.8.0;

contract ExampleContract {
function greet() external pure returns (string memory) {
return "Hello, World!";
}
}

The greet function is marked as external. It can only be called from outside the contract, such as from another contract. It's not accessible internally.

Conclusion

These modifiers help you control who can access your contract’s functions and state variables, ensuring that your DApps operate as intended while maintaining the security of the Ethereum blockchain.

Note::

👋 Hey there! If you have any burning questions or just want to say hi, don’t be shy — I’m only a message away. 💬 You can reach me at jamilkashem@zoho.com.

🤝 By the way, I think we share the same interest in software engineering — let’s connect on LinkedIn! 💻

--

--

MD Jamil Kashem Porosh

Software Engineer | 📝 Tech Blogger | React.js, React Native, JavaScript, Go, Python. (✉️ jamilkashem@zoho.com)