Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

solidity

What is the visibility of variables in Solidity?

Fatima Numan

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Solidity is an object-oriented programming language for designing smart blockchain contracts.

Visibility of variables

Solidity offers programmers ways to select the visibility of variables within a contract based on the user's needs.

The visibility of variables can be set in three ways in Solidity:

  • Public
  • Private
  • Internal

Let's discuss them in detail.

The visibility types of variables in Solidity.

Public

The contract containing it and all the other smart contracts can access a public variable. We can create a getter for the public variable, but not any setter. The variable declared public cannot be modified externally.

Private

The contract can only access a private variable it resides within. No other contract can access or modify it.

Internal

The contract can only access an internal variable it was declared in or by the same contract’s derived contracts or subclasses.

Syntax

To define the visibility for any variable, we use the following syntax:

data_type visibility variable_name
  • The data_type can be int, uint, string, etc.
  • The visibility can be public, private or internal.
  • The variable_name is the name of the variable specified by the user.

Note: The data_type always comes before the visibility of the variable.

Code

pragma solidity ^0.5.0;
/// defined contract Class A
contract Class_A{
/// public member defined for Class A
int256 public A1;
/// private member defined for Class A
int private A2 = 1;
/// internal member defined for Class A
int internal A3 = 3;
/// creating a constructor for Class A
constructor() public {
A1 = 2; // using a state variable
}
/// an internal function that returns the difference between the members A1 and A2
function subtract() public view returns (int256){
return A1 - A2;
}
/// an internal function that returns the sum between two numbers a and b
function Add(int a, int b) internal pure returns (int){
return a + b;
}
}
/// defined contract Class B as child of Class A
contract Class_B is Class_A {
/// a function that calls an internal function from Class A
function Call_A_for_B() public view returns (int){
/// used internal data member of Class A
int answer = Add(A3, 2);
return answer;
}
}
Demonstrating the visibility of variables in Solidity

Explanation

  • Line 1: We use pragma to specify the version of Solidity.
  • Line 7: We define a public member A1 for Class_A.
  • Line 9: We define a private member A2 for Class_A.
  • Line 11: We define an internal member A3 for Class_A.
  • Lines 13–15: We declare a constructor for Class_A where we specify the value for the public member A1.
  • Lines 17–19: We define a public function for subtracting the members A1 and A2 of Class A.
  • Lines 29–34: We define a function named Call_A_for_B() and call the function defined in Class_A inside it for addition. We pass the internal member A3 of Class_A as the argument.

RELATED TAGS

solidity

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring