
- Solidity Tutorial
- Solidity - Home
- Solidity - Overview
- Solidity - Environment Setup
- Solidity - Basic Syntax
- Solidity - First Application
- Solidity - Comments
- Solidity - Types
- Solidity - Variables
- Solidity - Variable Scope
- Solidity - Operators
- Solidity - Loops
- Solidity - Decision Making
- Solidity - Strings
- Solidity - Arrays
- Solidity - Enums
- Solidity - Structs
- Solidity - Mappings
- Solidity - Conversions
- Solidity - Ether Units
- Solidity - Special Variables
- Solidity - Style Guide
- Solidity Functions
- Solidity - Functions
- Solidity - Function Modifiers
- Solidity - View Functions
- Solidity - Pure Functions
- Solidity - Fallback Function
- Function Overloading
- Mathematical Functions
- Cryptographic Functions
- Solidity Common Patterns
- Solidity - Withdrawal Pattern
- Solidity - Restricted Access
- Solidity Advanced
- Solidity - Contracts
- Solidity - Inheritance
- Solidity - Constructors
- Solidity - Abstract Contracts
- Solidity - Interfaces
- Solidity - Libraries
- Solidity - Assembly
- Solidity - Events
- Solidity - Error Handling
- Solidity Useful Resources
- Solidity - Quick Guide
- Solidity - Useful Resources
- Solidity - Discussion
Solidity - Variable Scope
Scope of local variables is limited to function in which they are defined but State variables can have three types of scopes.
Public − Public state variables can be accessed internally as well as via messages. For a public state variable, an automatic getter function is generated.
Internal − Internal state variables can be accessed only internally from the current contract or contract deriving from it without using this.
Private − Private state variables can be accessed only internally from the current contract they are defined not in the derived contract from it.
Example
pragma solidity ^0.5.0; contract C { uint public data = 30; uint internal iData= 10; function x() public returns (uint) { data = 3; // internal access return data; } } contract Caller { C c = new C(); function f() public view returns (uint) { return c.data(); //external access } } contract D is C { function y() public returns (uint) { iData = 3; // internal access return iData; } function getResult() public view returns(uint){ uint a = 1; // local variable uint b = 2; uint result = a + b; return storedData; //access the state variable } }
Advertisements