logoAcademy

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