State Variables and Data Types
A state variable is permanently stored on the blockchain as part of the contract's data, every write to one costs real money (gas), which shapes how Solidity code is written more than almost anything else.
contract Wallet {
uint256 public balance; // unsigned integer, 256 bits
address public owner; // an Ethereum address
bool public isLocked;
string public label = "Savings";
constructor() {
owner = msg.sender; // the account that deployed the contract
balance = 0;
}
}
Core types
| Type | Meaning |
|---|---|
uint256 | Unsigned integer, 0 or positive, the most common numeric type |
int256 | Signed integer, can be negative |
address | A 20-byte Ethereum account or contract address |
bool | true or false |
string / bytes | Text or raw byte data |
Solidity has no floating-point type at all, financial logic is done in whole-number units (like storing cents instead of dollars) to avoid the rounding errors that plague floating-point money math in other languages.
msg.sender
msg.sender is a special, always-available variable holding the address that called the current function, it's the foundation almost every access-control check in Solidity is built on, "is the caller allowed to do this?"
function withdraw() public {
require(msg.sender == owner, "Not the owner");
// ...
}