Solidity Variable Keywords

Kaan Kaçar
Coinmonks
4 min readMay 25, 2023

--

One of the fundamental concepts you must grasp as a web3 developer is the usage of variable keywords. Variables are essential elements in any programming language, allowing you to store, manipulate, and retrieve data. In this guide, we will explore the various variable keywords in Solidity.

  1. Declaring Variables in Solidity: In Solidity, variables are declared using the type keyword, followed by the variable name. For example:
uint256 public myVariable;

This declares a variable named myVariable of type uint256, which is an unsigned integer capable of holding 256 bits of data. The public keyword is a modifier, which we will discuss later.

2. Value Types: Solidity provides several value types that allow you to work with different kinds of data. Let’s explore some of the commonly used value types:

2.1. Boolean: A boolean variable can store either true or false. For example:

bool public isActivated = true;

2.2. Integer Types: Solidity provides various integer types, such as uint8, int256, etc., with different bit sizes and signedness. For example:

uint8 public myUint8 = 255;
int256 public myInt256 = -42;

2.3. Unsigned Integer Types: Unsigned integer types, denoted by the uint keyword, can only store non-negative values. For example:

uint public myUint = 123;

2.4. Address: The address type is used to represent addresses. It can store both externally owned addresses and contract addresses. For example:

address public myAddress = 0x1234567890123456789012345678901234567890;

2.5. Fixed-Point Numbers: Solidity supports fixed-point numbers with various precisions using the fixed and ufixed keywords. For example:

fixed public myFixed = 1.234;
ufixed public myUfixed = 0.567;

Fixed point numbers are not fully supported by Solidity yet. They can be declared, but cannot be assigned to or from.

2.6. Enum: Enums allow you to define custom types with a finite set of values. Each value is assigned an index starting from 0. For example:

enum State { PENDING, APPROVED, REJECTED }
State public currentState = State.PENDING;

2.7. Bytes and Static Arrays: The bytes type allows you to work with arbitrary-length byte arrays, while static arrays have a fixed length. For example:

bytes public myBytes = "Hello, World!";
bytes32[5] public myStaticArray;

2.8. Dynamic Arrays: Dynamic arrays can have a variable length that can be changed during runtime. For example:

uint[] public myDynamicArray;

3. Reference Types: Solidity also provides reference types that allow you to work with complex data structures. Let’s explore some of them:

3.1. Structs: Structs allow you to define custom data structures with multiple fields. For example:

struct Person {
string name;
uint age;
}
Person public myPerson;

3.2. Mappings: Mappings are key-value stores that can associate a value with a unique key. For example:

mapping(address => uint) public balances;

3.3. Arrays: Arrays can store multiple values of the same type. They can be static or dynamic, similar to value types. For example:

Person[] public people;

3.4. Strings: The string type allows you to work with variable-length string data. For example:

string public greeting = "Hello, World!";

4. Variable Modifiers: Modifiers in Solidity provide additional functionality to variables. Let’s explore some commonly used modifiers:

4.1. Public: A public variable generates an automatic getter function, allowing other contracts or users to access its value. For example:

uint public myVariable;

4.2. Private: A private variable can only be accessed from within the current contract. For example:

uint private myVariable;

4.3. Internal: An internal variable can be accessed from within the current contract and its derived contracts. For example:

uint internal myVariable;

4.4. External: An external variable can only be accessed externally, typically through function calls. For example:

uint external myVariable;

5. Constant and Immutable Variables: Solidity allows you to define constant and immutable variables, which are initialized at compile-time and cannot be modified afterward. For example:

uint256 constant public MY_CONSTANT = 42;
uint256 immutable public myImmutable = block.timestamp;

6. The ‘Memory’ and ‘Storage’ Keywords: In Solidity, you often need to specify whether a variable should be stored in memory or storage. The memory keyword is used for temporary data that disappears after function execution, while the storage keyword refers to persistent contract storage. For example:

function myFunction() public {
uint[] memory myArray; // Declares an array in memory
myArray.push(42);
}

7. Global Variables: Solidity provides several global variables that can be accessed throughout your contract. Let’s explore a few of them:

7.1. msg: The msg global variable provides information about the current transaction. For example:

address public sender = msg.sender;
uint public value = msg.value;

7.2. block: The block global variable provides information about the current block. For example:

uint public blockNumber = block.number;
uint public blockTimestamp = block.timestamp;

7.3. tx: The tx global variable provides information about the current transaction. For example:

bytes32 public txHash = tx.hash;
address public origin = tx.origin;

7.4. now: The now global variable returns the current timestamp. For example:

uint public currentTime = now;

Final Words:

In this blog post, we covered the basics of variable keywords in Solidity for Web3 developers. By delving into value types, reference types, variable modifiers, constant and immutable variables, memory and storage keywords, and global variables, we aimed to provide a thorough understanding of how to utilize variables effectively in your smart contracts. Feel free to experiment and explore more advanced features as you continue your journey.

If you found this article informative and helpful, please consider following me for more blockchain and cryptocurrency-related content. You can also subscribe to my email list to receive updates on my latest articles and projects.

--

--