Variables
Learn about variables in Solidity.
We'll cover the following
In Solidity, there are three different types of variables:
State variables: These are kept in a contract storage facility permanently.
Local variables: These retain their values while a function is running.
Global variables: These are used to access information about the blockchain.
The state or local variable type must be declared because Solidity is a statically typed language: based on the variable type, every defined variable has a default value.
Guidelines for variable naming
Avoid using reserved keywords: Variable names shouldn’t contain any reserved keywords. For example, we can’t use keywords like
contract
orfunction
as variable names.Start with a letter or underscore: Variable names must begin with a letter or an underscore (
_
). For instance,_count
,name
, and_value
are valid variable names.Include letters and characters: Variable names can also include characters and letters from a to z or A to Z. For instance,
totalAmount
,user_name
, andmyVariable
are valid variable names.Focus on case sensitivity: Variable names are case-sensitive. This means that variables named
amount
andAmount
are treated as distinct variables.
Declaring a variable
The data type must be specified before the access modifier when declaring a variable. For example:
Get hands-on with 1200+ tech skills courses.