Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Visibility - function scope
  • Visibility Variables
  1. STEP 3

29: Visibility

Visibility - function scope

Solidity provides four access levels, which are related to the visibility of functions and state variables. They are:

public:

Everyone can access this, even from outside of the contract.

internal:

This can only be accessed from within the same contract or contracts inheriting from it.

private:

This can only be accessed from within the same contract.

external:

This can only be accessed from outside of the contract.


Examples Let's look at a few examples of visibility in action.

  • Public Here's an example of a public variable:

contract MyContract {    
    uint public myPublicVariable;
}

This variable can be accessed from within the contract, as well as from other contracts and externally.

  • Internal Here's an example of an internal variable:

contract MyContract {    
    uint internal myInternalVariable;
}

This variable can only be accessed from within the same contract, or from contracts that inherit from it.

  • Private Here's an example of a private variable:

contract MyContract {    
    uint private myPrivateVariable;
}

This variable can only be accessed from within the same contract.

  • External Finally, here's an example of an external function:

contract MyContract {    
    function myExternalFunction() external {        // do something    }
}

This function can only be called from outside of the contract.


Visibility Variables

Visibility variables are a concept in Solidity which allow you to control the visibility and access of variables and functions. They are important to keep in mind when creating contracts, as they are used to control the scope of the program.

There are three visibility variables in Solidity:

Public: Public visibility allows a variable or function to be visible and accessible both inside and outside the contract.

Internal: Internal visibility allows a variable or function to be visible and accessible only within the contract.

Private: Private visibility allows a variable or function to be visible and accessible only within the contract but not outside.

Examples

Here are some examples of visibility variables in Solidity:

  • Public

contract Example {  
    uint public myVar;  // myVar is publicly accessible    
    function getMyVar() public view returns (uint) {    
        return myVar;  
        // myVar can be accessed both inside and outside the contract  
    }
}
  • Internal

contract Example {  
    // myVar is only accessible within the contract    
    uint internal myVar;  
    function getMyVar() internal view returns (uint) {    
        // myVar can only be accessed within the contract  
        return myVar;  
    }
}
  • Private

contract Example {  
    // myVar is only accessible within the contract    
    uint private myVar;  
    function getMyVar() private view returns (uint) {    
        // myVar can only be accessed within the contract  
        return myVar;  
    }
}

That's it for the lesson 29! In the next lesson, Interface

Previous28: Super KeywordNext30: Interface

Last updated 1 year ago