Contract Constructor

More about Solidity

As a segue to learning about the object-oriented programming aspect of Solidity, we will touch upon contract constructors. Similar to class constructors in other languages, constructors in Solidity allow us to define the state of a contract at the time of initialization.

Syntax

Below is the general syntax for a contract constructor:

constructor(<arguments>) {      
}

Example

Below is an example of a contract constructor in action:

contract A {
    uint num;
  
    constructor(uint _num) {
        num = _num;
    }
}

On this page

Privacy Policy

We respect your privacy and are committed to protecting your personal data. This privacy policy will inform you about how we look after your personal data and tell you about your privacy rights.

Read full policy