Difference between Public vs Internal vs Private state variables in solidity programming langauge with example ?

 Difference between Public vs Internal vs Private state variables in solidity programming langauge with example ?


Public state variables can be accessed internally as well as via messages. For a public state variable, an automatic getter function is generated.


 Internal state variables can be accessed only internally from the current contract or contract deriving from it without using this.


 Private state variables can be accessed only internally from the current contract they are defined not in the derived contract from it.


Example of  Public vs Internal vs Private state variables


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

   }

}



Comments

Last 7 Days

How to use referrerpolicy Attribute value ( no-referrer-when-downgrade ) with < iframe > tag in HTML ? with example

Smart contract code for Anyone can send coins to each other without a need for registering with a username and password with help of Ethereum keypair

How Artificial Intelligence Is Changing the World: A Complete Pro Guide

Why People Use iLovePDF Merger and Better Online Alternatives

Why Converting PDF to Text Improves Productivity (With Examples)

How Text Conversion Tools Work and Why They Are Useful

Why People Use PDF Merger Tools for Document Management

How to Convert Image to PDF on iPhone (Free & Easy Methods) | ThinkforU

How to Clean and Format Text Online Easily