Skip to Content
Ethereum Cookbook
book

Ethereum Cookbook

by Manoj P R
August 2018
Intermediate to advanced
404 pages
11h 19m
English
Packt Publishing
Content preview from Ethereum Cookbook

Constructor

The constructor is a function that is executed when a contract is created. It cannot be called explicitly and is invoked only once during its lifetime. Every contract can have exactly one constructor and if no constructor is declared, the contract will use the default constructor.

  1. Create a constructor in solidity using the constructor keyword:
contract A {    constructor() {        // Constructor    }}
  1.  Then, write some logic that should get executed during contract creation inside the constructor:
contract A {    address owner;    constructor() {        owner = msg.sender;    }}
  1.  Now, decorate the constructor as either public (default) or internal. A constructor set as internal causes the contract to be marked as abstract:
contract C {    uint n; constructor(uint ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Building Ethereum Dapps

Building Ethereum Dapps

Roberto Infante
Ethereum For Dummies

Ethereum For Dummies

Michael G. Solomon

Publisher Resources

ISBN: 9781789133998Supplemental Content