Solidity Immutable

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

The immutable keyword is used for variables that act very similar to constants, however they can be initialized with a constructor.

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

contract exampleOfImmutable {
    // usually constant variables are capitalized
    address public immutable THE_ADDRESS;
    string public immutable MY_DOMAIN;

    constructor(string memory _name) {
        THE_ADDRESS = msg.sender;
        MY_DOMAIN = _name;
    }
}

After these immutable variables are created and changed during deployment, they can never be changed again. The purpose of this is to prevent vulnerabilities from editing an important variable, while allowing the developer to edit the variable upon deployment (unlike Constants).

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.