Rise In Logo




Build on Scroll

Using Variables

Variables are the building blocks of your smart contract's logic. They can be updated and manipulated through functions. You can store, update, and retrieve these variables to keep track of data or change the state of your contract.

Understanding Variables in Solidity: Public vs. Private

In your journey through Solidity, understanding how to properly declare and utilize variables is crucial. Variables are not just simple placeholders; they dictate how data flows through your smart contracts. To manage this flow effectively, Solidity provides different levels of access to these variables, specifically through the use of visibility specifiers such as public and private. Let's break these down and see how they impact the behavior of your smart contracts.

Public Variables

Declaring a variable as public makes it accessible from outside the contract. When you declare a variable public, Solidity automatically creates a getter function for it. This means anyone or any other contract can read the value of this variable, but they can't modify it directly unless you provide a function to do so.

Here's how you might declare a public variable:

uint public totalSupply;

In this example, totalSupply is a uint (unsigned integer) that is accessible to anyone who interacts with the contract. Since it's public, it's transparent and open, much like a display in a store window.

A quick note, if the variable is public, automatically a getter function will be created for it by Solidity. This is how the variable will be accessible from outside of the contract.

Private Variables

Private variables, on the other hand, are only accessible within the contract itself. No other contract or user can view or modify these variables directly, regardless of how the contracts interact. This is useful for sensitive data or internal mechanisms that you do not want to expose to the outside world.

Here's an example of a private variable:

uint private secretNumber;

In this case, secretNumber is hidden from external access, much like documents in a locked drawer. Only functions within the same contract can access or modify this variable. If you want to read data of a private variable from outside of the contract, then you will need to create a function for it unlike the previous public example.

Using Public and Private Variables

When you're building a smart contract, deciding between public and private variables can influence both security and functionality. Here are examples that show how to use these variables in a contract:

pragma solidity ^0.8.0;

contract MyContract {

    uint private secretValue = 42;

    uint public openValue = 123;

    // Function to access the private variable

    function revealSecret() public view returns(uint) {

        return secretValue;

    }

    // Function to change the private variable

    function changeSecret(uint \_newSecret) public {

        secretValue = \_newSecret;

    }

    // Note: No need for a getter function for `openValue` because it's public

}

In this contract:

  • secretValue is private, so it is only accessible via a function within the contract (`revealSecret` and changeSecret).
  • openValue is public, automatically providing a getter function that lets anyone check its value.

Summary

Public and private variables play a crucial role in determining how data is accessed and manipulated in your Solidity contracts. Public variables enhance transparency but can limit control over data manipulation, while private variables secure your data but reduce readability and accessibility from outside the contract.

Choosing the right type of variable visibility depends on the specific needs and security requirements of your contract. As you become more familiar with these concepts, you'll better understand how to balance openness with security in your smart contract designs.

So far, you have learned some important concepts in Solidity. In the next lesson, you will learn about functions and modifiers.

It can be hard to understand just by looking at explanations. For that reason, after you learn functions and modifiers, you will see an example about these concepts before moving forward. At the end of the Solidity lessons, you will see a smart contract demonstration that will wrap the whole course for you.

Comments

Anonymous

0/500

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!