Constants

State variables can be declared as constant. Value of constant variables must be set before compilation and it cannot be modified after the contract is compiled.

Why use constants?

Compared to state variables, constant variables use less gas.

Style guide

Following convention, constants should be named with all capital letters with underscore separating words.

THIS_IS_MY_CONSTANT_VAR
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Constants {
    uint public constant MY_UINT = 123;
    address public constant MY_ADDR =
        0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc;
}

Last updated

Was this helpful?