Learning Objectives – after this chapter you will be able to:
  • Explain when a constructor runs and what it is used for.
  • Write constructors with parameters, payable constructors and immutable assignments.
  • Pass arguments to base-contract constructors in two ways.
What are Constructors?
  • A constructor is a special function, declared with the constructor keyword, that contains the initialisation logic for a contract’s state variables.
  • It runs only once – when the contract is deployed – and can never be called again.
  • It is optional. If no constructor is written, the compiler uses a default constructor that does nothing (equivalent to constructor() {}).
  • Since Solidity 0.7.0 a constructor has no visibility (public/internal are no longer used). To stop a contract from being deployed on its own, mark the contract abstract.
  • When a contract inherits from a parent whose constructor needs arguments, the child must supply them; otherwise the child must be declared abstract.
constructor(<parameters>) [payable] {
    // initialisation code
}
Key Points
  • A contract can have only one constructor – constructor overloading is not supported.
  • It is executed once to initialise the contract’s state; arguments are supplied in the deployment transaction (in Remix, in the box next to Deploy).
  • The constructor code itself is not stored on the blockchain; only the final runtime code (all functions reachable after deployment) is deployed.
  • A constructor can be payable, allowing Ether to be sent during deployment.
  • immutable variables are normally assigned in the constructor.
When to Use a Constructor

Use the constructor to set values that must be fixed or known from the start, for example:

  • Contract owner – store msg.sender as the owner/admin at creation time (used in all course projects as blockAdmin or auctionManager).
  • Limits – a maximum supply, ticket price or number of participants.
  • Initial data – a list of candidates, a token name and symbol.
  • Configuration flags – any true/false setting the contract needs.
Example : Constructors and Base Constructors
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Owned {
    address public owner;

    constructor(address _owner) {
        owner = _owner;
    }
}

// Way 1: pass the base constructor argument in the inheritance list
contract FixedOwner is Owned(address(0x1234)) {}

// Way 2: pass it in the child's constructor (useful when it depends on inputs)
contract Course is Owned {
    string public name;
    uint public immutable maxSeats;        // assigned once, here
    uint public createdAt;

    constructor(string memory _name, uint _maxSeats) payable Owned(msg.sender) {
        require(_maxSeats > 0, "Seats must be positive");
        name = _name;
        maxSeats = _maxSeats;
        createdAt = block.timestamp;
    }

    function balance() external view returns (uint) {
        return address(this).balance;      // Ether sent at deployment (payable constructor)
    }
}
Key Takeaways
  • The constructor runs once at deployment and is optional; only one is allowed.
  • No visibility keyword is used; mark contracts abstract to prevent direct deployment.
  • Base constructor arguments go in the inheritance list or in the child constructor’s header.
Download | Print      E-Notes

The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.

Video : Constructors in Solidity
Resource File
Question / Answer (15 questions)

Answer : A constructor is a special function declared with the constructor keyword that initialises a contract’s state. It runs exactly once, when the contract is deployed.

Answer : No. It runs only during deployment, and its code is not part of the deployed runtime bytecode, so it can never be called again.

Answer : The compiler provides a default constructor that does nothing, equivalent to constructor() {}.

Answer : Only one. Solidity does not support constructor overloading.

Answer : Constructors set up the contract’s initial state at deployment – for example the owner address, limits or initial data – using values provided at deployment time. This guarantees that the contract starts in a valid state.

Answer : Whenever values must be set at creation: the contract owner (msg.sender), a maximum token supply, a ticket price, an initial candidate list, or configuration flags.

Answer : No. Since Solidity 0.7.0 constructor visibility is no longer used. To prevent a contract from being deployed on its own, it is marked abstract instead.

Answer : A constructor marked payable can receive Ether in the deployment transaction, so the contract starts with a balance.

Answer : An immutable value can be set only once during construction; after deployment it is fixed and stored in the contract code, making reads cheap.

Answer : In the inheritance list, e.g. contract B is A(10) {}, or in the derived constructor’s header, e.g. constructor(uint x) A(x) { }.

Answer : The derived contract must be declared abstract; it cannot be deployed until a further derived contract provides the arguments.

Answer : Remix shows input fields next to the Deploy button; the values entered there are encoded into the deployment transaction.

Answer : No. Only the runtime code that results from running the constructor is stored at the contract’s address.

Answer : msg.sender – the account that deployed the contract – because the Course constructor passes msg.sender to the Owned base constructor.

Answer : Because the constructor runs only once; if invalid values are stored at deployment, the contract may be permanently misconfigured. A require check rejects the deployment instead.
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. A constructor runs:

Show Answer

Correct Answer : B. Once, at deployment
Constructors execute only during deployment.

Q2. How many constructors can a Solidity contract have?

Show Answer

Correct Answer : A. One
Constructor overloading is not supported.

Q3. Which keyword lets a constructor receive Ether during deployment?

Show Answer

Correct Answer : D. payable
A payable constructor can accept Ether.

Q4. Since Solidity 0.7.0, to prevent direct deployment of a base contract you:

Show Answer

Correct Answer : C. Mark the contract abstract
Constructor visibility is no longer used; use abstract.

Q5. contract B is A(10) {} passes 10 to:

Show Answer

Correct Answer : A. A’s constructor
Arguments in the inheritance list go to the base constructor.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. Solidity Team. (n.d.). Contracts (creating contracts, visibility and getters, function modifiers, constant and immutable, functions, events, custom errors, inheritance, abstract contracts, interfaces, libraries). Solidity Documentation. https://docs.soliditylang.org/en/latest/contracts.html
  2. Solidity Team. (n.d.). Types (value types, reference types, mappings, operators, conversions). Solidity Documentation. https://docs.soliditylang.org/en/latest/types.html
  3. CryptoMarketPool. (n.d.). Constructor in Solidity smart contracts. https://cryptomarketpool.com/constructor-in-solidity-smart-contracts/
  4. Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.