Solidity Series

Solidity Programming Language – Immutability

In Solidity, the immutable keyword is used to declare state variables whose values cannot be changed after they are initialized. These variables are similar to constants but are initialized at deployment time rather than at compile time.

Once assigned a value in the constructor or at declaration (for state variables), the value of an immutable variable cannot be modified throughout the lifetime of the contract. This immutability guarantees that the value remains constant and cannot be altered by any means, providing security and predictability to the contract’s behavior.

In case of constant state variable -> It is inline (means we need to initialize at the time of declaration).

To over come this problem where we can make our state variable and initialize its value at the time of deployment.

In case of immutability we can initialize inline and also through constructor.

Here the cost ->

1) Simple (23534)

2) Immutable (21526)

3) Constant (21472)

Code:

Output:

Hi, I’m saksham dixit

Leave a Reply

Your email address will not be published. Required fields are marked *