Sign in
Log inSign up
Solidity Constant & Immutable

Solidity Constant & Immutable

Oyindamola Abiola's photo
Oyindamola Abiola
·Apr 23, 2022·

3 min read

In Solidity there 2 types of fixed state variables these are Constant and Immutable, from the keywords you have an idea why they are classified as fixed variables, these are types of variables that cannot be modified after the contract is deployed, both can only be declared as a state variable, you cannot have them declared in the function, but they are accessible anywhere across the contract. There is a need to be careful here, while using either of these two keywords, be sure you know what you're doing, to avoid shooting yourself in the foot. Mind you, they come with the benefit of lower gas cost, cute right? But still, be careful, consider the need to upgrade your contract in the future, if for example there is a vulnerability.

Constant Variable

This is declared using the constant keyword and by convention, the variable name should be all capital, see below.

code sample

// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.0;
contract TestConstant {

uint256 public constant TOTAL = 10000; 
string public constant MY_NAME = "Oyindamola";

}

The constant variable must be declared and assigned a value before deploying the contract because it needs to be initialized at compile time. Compared to the regular state variables, the gas costs of constant variables are much lower, copy and deploy the code below on your remix, call the variables to check for the gas cost differences, as at the time of writing this article those comments in the code were the gas cost;

code sample

// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.0;

contract TestConstant {

    uint256 public constant TOTAL = 10000; //  gas 21393
    uint256 public age = 73; // gas 23471

    string public constant MY_NAME = "Oyindamola"; // gas 21863
    string public dog_Name = "Tito"; // gas 24541

}

Immutable Variable

Similar to a constant variable, an immutable variable is declared using the immutable keyword, by convention, the variable name should be all capital, see below;

Code sample

// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.0;

contract TestImmutable {
    address public immutable OWNER;
    uint public immutable TOTALSUPPLY;

    constructor() {
        OWNER = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; 
        TOTALSUPPLY = 100000;
    }
}

Immutable variables can be declared and the value is assigned in the constructor like in the code sample above, this is because immutable variables can only be initialised once. Also, the gas cost is lower compared to the regular state variables, copy and deploy the code below on your remix, call the variables in the contracts to check for the gas cost differences, as at the time of writing this article those comments in the code below were the gas cost;

code sample

// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.0;

contract TestImmutable {
    address public immutable OWNER;
    uint public immutable TOTALSUPPLY;

    constructor() {
        OWNER = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; // 21420
        TOTALSUPPLY = 100000; // 21393
    }
}


contract TestImmutable2 {
    address public owner;
    uint public totalSupply;

    constructor() {
        owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; // 23575
        totalSupply = 100000; // 23471
    }
}

Recap:

Constant and Immutable variables are similar in terms of being fixed values (i.e cannot be changed), they are similarly declared with their keywords but differently initialized. According to the documentation, not all types of constants and immutables are implemented at this time. The only supported types are strings (only for constants) and value types.

Please feel free to comment below. Thanks for reading