Back
Lesson 15:
Constructor
Introduction to constructors and how to use it in your smart contracts.
Visit desktop version for better experiences.
Constructor
A constructor
is an optional function declared upon contract creation.
If there is no constructor
, the contract will assume the default constructor, which is equivalent to constructor() {}
.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// Simple contract with a constructor
contract SimpleContract {
uint256 public data;
constructor(uint256 _data) {
data = _data; // Initialize 'data' with the value passed to the constructor
}
}
// Contract with a constructor and a function to change the data
contract ChangeableContract {
uint256 public data;
constructor(uint256 _data) {
data = _data; // Initialize 'data' with the value passed to the constructor
}
function setData(uint256 _data) public {
data = _data; // Change 'data' to the new value passed to the function
}
}
// Base contract A
contract A {
string public name;
constructor(string memory _name) {
name = _name; // Initialize 'name' with the value passed to the constructor
}
}
// Contract B inherits from A and has its own data
contract B is A {
uint256 public data;
constructor(string memory _name, uint256 _data) A(_name) {
data = _data; // Initialize 'data' with the value passed to the constructor
}
}
// Parent constructors are always called in the order of inheritance
// regardless of the order of parent contracts listed in the
// constructor of the child contract.
// Order of constructors called:
// 1. A
// 2. B
// 3. C
contract C is A, B {
constructor(string memory _name, uint256 _data) A(_name) B(_name, _data) {}
}