State Variables

In Solidity, state variables are variables that are permanently stored in contract storage. They represent the state of the contract and can be accessed and modified by all functions within the contract.
State variables are defined at the contract level, outside of any function, and can have different types such as bool, int, uint, address, struct, mapping, etc.
State variables are initialized with a default value, which depends on the variable type. For example, a bool variable is initialized to false, an integer variable is initialized to 0, and an address variable is initialized to 0x0.
State variables are also accessible outside of the contract, through its ABI (Application Binary Interface) and its address on the blockchain.
It's important to note that state variables are permanent, so any changes made to them will be stored on the blockchain permanently. This makes it important to carefully consider the implications of any changes made to state variables, as they cannot be easily modified or removed.
pragma solidity ^0.8.0;
contract MyContract { string public greeting = "Hello, World!"; uint256 public myNumber = 42; address public owner;
pragma solidity ^0.8.0;
​
contract MyContract {
string public greeting = "Hello, World!";
uint256 public myNumber = 42;
address public owner;
​
constructor(address _owner) {
owner = _owner;
}
​
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
​
function setMyNumber(uint256 _number) public {
myNumber = _number;
}
}
Last modified 11mo ago