In this article, we are going to understand the variables and their usage in Solidity. In our previous article Solidity For Beginners, we’ve seen the basic introduction of the Remix IDE and deployed our first smart contract. If you’re new here, I would encourage you to read the previous article first.
The latest stable release of Solidity is 0.5.0, and that has breaking changes. There is a strong possibility that the old code would not run with the latest version. With this note, let’s get started with variables and types.
Access Modifier
Access Modifiers are the keywords used to specify the declared accessibility of a function or a type. There are four access modifiers available in Solidity.
Public
The Public element can be inherited and can be accessed by external elements. All can access a public element.
Private
The Private element doesn’t get inherited and can't be accessed by external elements. It can be accessed from the current contract instance only.
Internal
The Internal element can be inherited but can’t be accessed by external elements. Only the base contract and derived contract can access internal element.
External
The External element can’t be inherited but it can be accessed by external elements. Current contract instance can’t access external element, it can be accessed externally only.
Variable Declaration
Variable declaration in Solidity is a bit different; you have to specify the type (data type) first, followed with an access modifier, and the variable name. If you would not specify any access modifier, it will be considered as a private.
Structure
- <type> <access modifier> <variable name> ;
- uint public temp;
State variable
State variables store the value permanently in the contract storage. Consider that you’re using C# or other languages and you want to store user information for a long time, what would you do? Connect your application with a database server and then save that information to the DB. However, in Solidity, no need to make a connection, you can easily store your data permanently with just the use of state variables.
Each method has its own scope, and the State variables should define outside of the scope of any defined functions.

Local Variable
The context of a local variable is within the function, and it cannot be accessed outside. Usually, these variables are used to hold temporary values for processing or calculating something. “temp” in the upper screen is a local variable, which we cannot use outside of “addition” function.
Furthermore, there is a difference in a storage location between state and local variables that we’ll cover in a separate article.
Types
Solidity is a statically typed language, which means the type of each variable needs to be specified. Declared types have some default values, typically called “zero-state”. For instance, the default value for bool is false.
There is no concept of null or undefined in Solidity! Sounds great, no need to handle null reference exception here :D
Same as other languages, there are two kinds of types in Solidity: value types and reference types.



Ano MepaniPosted Jan 10, 2019, 1:51 AM
Very useful article for getting started learning Solidity.
Mahesh ChandPosted Jan 9, 2019, 10:40 PM
Great writing style. You've done extra work including adding link to the previous and next articles. Cheers!