0Pricing
Dart Academy · Lesson

Default and Parameterized Constructors

Initialize fields when creating objects.

Default and Parameterized Constructors is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What a Constructor Does

A constructor is the special method that builds a new object and sets up its starting state when you write the class name. 🏗️

The Default Constructor

If you write no constructor at all, Dart gives you a free default constructor that takes no arguments and creates a bare object.

class Dog {}
var d = Dog(); // uses the default constructor

A Parameterized Constructor

Add parameters so the caller can supply values up front. A parameterized constructor assigns those values to the object fields.

class Dog {
  String name;
  Dog(this.name);
}

The this Keyword in Parameters

The shorthand this.name in a parameter means take the argument and store it straight into the field of the same name, saving you a line.

class Dog {
  String name;
  Dog(this.name);
}

Without the this Shortcut

You can assign by hand inside the body instead. Here this separates the field from the parameter when their names clash.

class Dog {
  String name;
  Dog(String name) {
    this.name = name;
  }
}

Creating an Object

Call the class like a function and pass arguments in order. Dart runs the constructor and hands you a ready instance.

var d = Dog("Rex");
print(d.name); // Rex

Several Parameters in Order

List parameters left to right; callers must pass them in the same positional order, which is great for two or three clear fields.

class Point {
  int x, y;
  Point(this.x, this.y);
}

Adding Logic to the Body

A constructor body can run extra setup after the fields are set, such as validating or logging. Keep this body small and focused.

class Box {
  int size;
  Box(this.size) {
    print("Box made: $size");
  }
}

Constructors Have No Return Type

You never write a return type and never return a value. A constructor always hands back the new object, so the name alone is enough.

class Cat {
  Cat(); // no void, no return
}

One Unnamed Constructor Per Class

A class may have only a single unnamed constructor. Need more ways to build? You will reach for named constructors next.

Defaults Disappear Once You Add One

The moment you declare any constructor, Dart stops giving you the free no-arg one. Add it back yourself if you still want an empty build.

class User {
  String name;
  User(this.name);
  User.empty() : name = "";
}

Quick Check

Quick check on default versus parameterized constructors.

Recap: Building Objects

You met constructors: the free default one, parameterized ones, the this shortcut, and how declaring one removes the default. Up next, named constructors. ✨

Frequently asked questions

Is the “Default and Parameterized Constructors” lesson free?

Yes — the full text of “Default and Parameterized Constructors” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Default and Parameterized Constructors”?

Initialize fields when creating objects. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Default and Parameterized Constructors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Default and Parameterized Constructors
  2. Named Constructors for Clear Intent
  3. Initializer Lists and Redirecting
  4. Factory and const Constructors
← Back to Dart Academy