🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity

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;
    }
}
PreviousSolidityNextData Location

Last updated 2 years ago