Solidity is a statically typed programming language, Each variable you use in solidity must declare of type. Like any programming language, It is used to store the value in a variable.

Variables in solidity are used to store the data in blockchain as well as used in functions for processing.

This tutorials covers

  • Declare variable Syntax
  • Solidity variable examples

Solidity Variable Syntax

variables are declared with the type of variable and visibility or access modifier and variable name.

datatype  access-modifier variablename

Datatype is an inbuilt or custom user-defined data type such as boolean, unit256.

access-modifier: Also called visibility and how and where the variable values are accessible. Possible values are

  • public
  • private
  • internal
  • external

variablename is a valid identifier in solid, It contains a combination of alphabets and numeric. The reserved keywords are not used.

The variables can be declared in Contracts or functions

Variable type initial default values

creating a variable does assign with default initial values.

Based on data type, initial values are different as given.

These default values are assigned when variables are not assigned with any value.

Data Type Default value Value/Reference type
bool false Value type
string empty string("") Value type
int/uint 0 Value type
fixed/ufixed types 0.0 Value type
address 0x0000000000000000000000000000000000000000 Value type
enum first element of an enum constant Value type
Internal function It returns empty function, if it conatains retur, It return empty values Value type
external function function returns Value type
mapping blank empty mapping Reference type
struct returns an struct with initial default values Reference type
Fixed array All the items are default values Reference Type
Dynamic array Empty Array([]) Reference Type