Contract constructor
Solidity supports declaring a constructor within a contract. They are optional and the compiler induces a default constructor when none is explicitly defined.
The constructor is executed once while deploying the contract. It is quite different from other programming languages. In other languages, a constructor is executed whenever a new object instance is created. Deployment of the contract also happens using the new
keyword and each time it deploys a new contract instance with a new address. In short, constructor code is invoked every time a new contract instance is created using the new
keyword, or it is deployed using frameworks such as Truffle/Hardhat.
Constructors should be used to initialize state variables and set up the context. The constructor code is the initial code executed for a contract. There can be at most one constructor in a contract, unlike constructors in other programming languages. Constructors can take parameters and corresponding...