| Modifier | Within Contract | Derived Contract | Other Contract | External |
|---|---|---|---|---|
public |
✅ | ✅ | ✅ | ✅ |
internal |
✅ | ✅ | ❌ | ❌ |
private |
✅ | ❌ | ❌ | ❌ |
external |
❌ (via this) |
❌ | ✅ | ✅ |
Solidity uses four visibility modifiers to control how functions and state variables can be accessed:
Public - can be called from within the contract, other contracts, externally
External - can be called from other contracts, externally; NOT from within the same contract
Internal - can be called from within the same contract, derived contracts (inheritance), NOT from other contracts, NOT externally
Private - can be called from within the same contract, NOT from derived contracts, NOT from other contracts
Important notes:
- State variables can be public, internal, or private (NOT external)
- Functions can be public, internal, private, or external
- public state variables automatically generate getter functions
- external is more gas efficient than public because it doesn't create an internal function copy
- private and internal are NOT secure — blockchain data is always visible (just not accessible via function calls)
Best practices:
- Use external for functions meant to be called from outside
- Use internal for helper functions used by inheritance
- Use private with caution — usually only for helper functions
- Use public for state variables that need public access
- Be careful with private — it provides no security, just prevents accidental calls
Top comments (0)