Loading...
Loading...

public, private, or internal.uint256 — for positive integers (0, 1, 2, 100, 1000000...)int256 — for integers including negative (-100, -1, 0, 1, 100...)bool — for boolean values (true or false)string or string memory — for text ("Hello", "Alex", "My contract"...)address — for wallet and smart contract addressesbytes or bytes memory — for binary datapublic – the variable can be read by everyone. A getter function is automatically created, allowing everyone to read the variable value from outside the contract. For example, for uint256 public counter a function function counter() public view returns (uint256) will be created, which any contract or user can use to read the counter variable value.private – the variable is accessible for reading only within this contract, even inheriting contracts cannot read itinternal – the variable is accessible for reading within the contract and its inheritors (default for variables)public modifier only allows reading the variable from outside the contract, but doesn't affect the ability to modify it. Only functions inside the contract can modify the variable. To make a variable modifiable from outside, you need to create a special function for that.internal or private modifiers don't allow hiding or encrypting information! They only restrict the ability of other smart contracts to access it. In blockchain, all data is public, so state variables are not suitable for storing secret information.
contract SimpleStorage { // This is a variable named "message" that stores text. // Accessible only within the contract string message;
// This is a variable named "number" that stores a number. // Accessible only within the contract and its child contracts uint256 number;
// This is a variable named "publicNumber" that stores a number. // Accessible to everyone uint256 public publicNumber;
// This is a variable named "privateBool" that stores a boolean value – true or false. // Accessible only within the contract bool private privateBool;
// This is a variable named "internalAddress" that stores an address. // Accessible only within the contract and its child contracts address internal internalAddress;}
contract DefaultValues { // Numeric types are initialized to zero uint256 public number; // 0 int256 public signedNumber; // 0 uint8 public smallNumber; // 0
// Boolean type is initialized to false bool public flag; // false
// Address is initialized to zero address address public userAddress; // 0x0000000000000000000000000000000000000000
// Strings and bytes are initialized empty string public message; // "" bytes public data; // 0x (empty byte array)
// Arrays are initialized empty uint256[] public numbers; // [] address[] public users; // []}contract DirectInitialization { // Initialization immediately at declaration uint256 public counter = 100; // Counter starts at 100 string public greeting = "Hello, world!"; // Welcome message bool public isActive = true; // Contract is active by default address public admin = 0x1234567890AbcdEF1234567890AbcdEF12345678; // Fixed admin address
// Can use mathematical expressions uint256 public maxUsers = 1000 * 100; // Maximum 100,000 users uint256 public fee = 5; // 5% fee}contract ConstructorInitialization { // Variables that will be initialized in the constructor address public owner; uint256 public creationTime; uint256 public initialBalance; string public contractName;
// Constructor executes when creating the contract constructor(string memory _name, uint256 _balance) { owner = msg.sender; // Owner — the one who created the contract creationTime = block.timestamp; // Contract creation time initialBalance = _balance; // Initial balance passed as parameter contractName = _name; // Contract name passed as parameter }}contract TokenSettings { uint256 public maxSupply = 1000000; // Maximum number of tokens string public name = "MyToken"; // Token name uint8 public decimals = 18; // Number of decimal places uint256 public transferFee = 5; // Transfer fee (5%)}contract DynamicContract { address public owner; uint256 public creationTime; uint256 public initialSupply; string public customName;
constructor(string memory _name, uint256 _supply) { owner = msg.sender; // Depends on who creates the contract creationTime = block.timestamp; // Depends on creation time initialSupply = _supply; // Passed as parameter customName = _name; // Configured at creation }}contract UserStats { uint256 public score; // Automatically 0 — new users start from zero bool public isVerified; // Automatically false — users are not verified uint256 public lastLogin; // Automatically 0 — haven't logged in yet}contract GamePlatform { // === 1. INITIALIZATION AT DECLARATION === // Used for fixed game settings string public constant PLATFORM_NAME = "CryptoGameHub"; // Constant uint256 public maxPlayers = 1000; // Maximum players uint256 public entryFee = 0.01 ether; // Entry cost bool public isMaintenanceMode = false; // Maintenance mode disabled
// === 2. DEFAULT VALUES === // Used when zero values are suitable uint256 public totalGamesPlayed; // 0 - no games yet uint256 public totalPrizePool; // 0 - prize pool empty bool public seasonStarted; // false - season not started
// === 3. INITIALIZATION IN CONSTRUCTOR === // Used for dynamic values address public owner; // Platform owner uint256 public platformLaunchTime; // Platform launch time uint256 public seasonNumber; // Current season number string public welcomeMessage; // Personalized welcome
constructor(uint256 _seasonNumber, string memory _message) { // Values depending on contract creator owner = msg.sender; // The one who deployed the contract platformLaunchTime = block.timestamp; // Current blockchain time
// Values passed at creation seasonNumber = _seasonNumber; // Season number set at creation welcomeMessage = _message; // Message is configured }}Загрузка...


