In this track: Write and read Solidity
advanced · 4 MIN READ

Contract declarations and constructors

Creation-time setup differs from later runtime calls.

LET’S PICTURE IT

Setting up a classroom scoreboard once is different from updating its score during each lesson.

SEE IT FOR YOURSELF
A valid counter call, step by stepStep 1 / 4
A valid counter call, step by stepFunction requestincrement()No argumentsBefore executionstored count0A local teaching example with a defined starting stateIncoming callCheck function dispatchCheck coded conditionsRules passConditions passContinue executionA failed check can revert insteadCalculation0 + 1Result: 1Storage writecount = 1Persists if call succeedsCalculating a local value and storing it are separate stepsPersistent statecount1Later readGetter reports 1No new incrementState, logs and browser display have different roles

This example begins with count zero and a request to call increment(). No real contract is deployed.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

A declaration defines the type

The contract body groups its state and behavior. Deploying an instance gives it a particular address and state under the network’s creation rules.

A constructor performs initialization

The constructor runs during creation. Its parameters can establish initial values or permissions. Those choices are part of the deployed instance’s context.

It is not an ordinary later function

The constructor’s creation-time logic is not exposed as a callable runtime method named constructor. Upgradeable architectures often need a distinct initializer pattern with separate safety requirements.

Read deployment arguments

Two instances from similar source can begin with different values. Inspect constructor inputs and deployment results rather than assuming the source text alone supplies every instance-specific fact.

Read the example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract LearningExample {
    uint256 public count;

    constructor(uint256 initialCount) {
        count = initialCount;
    }
}

The constructor argument determines this instance’s starting count.

QUICK CHECK · NO PRESSURE

Can a deployed instance’s constructor be called again as an ordinary external function?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.