Constructors

Solidity supports declaring a constructor within a contract. Constructors are optional in Solidity and the compiler induces a default constructor when no constructor is explicitly defined. The constructor is executed once while deploying the contract. This is quite different from other programming languages. In other programming languages, a constructor is executed whenever a new object instance is created. However, in Solidity, a constructor is executed are deployed on EVM. Constructors should be used for initializing state variables and, generally, writing extensive Solidity code should be avoided. The constructor code is the first set of code that is executed for a contract. There can be at most one constructor in a contract, ...

Get Solidity Programming Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.