Menu
Coddy logo textTech

Constructor Basics

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 8 of 110.

A constructor is a special method that runs automatically when you create a new object. It initializes the object's instance variables right at the moment of creation.

Here is an example of a class with a constructor:

class Dog {
  String name;
  String breed;

  Dog(String name, String breed) {
    this.name = name;
    this.breed = breed;
  }
}

The constructor has the same name as the class. Parameters are passed when creating the object.

Create objects by passing arguments to the constructor:

Dog rex = Dog('Rex', 'German Shepherd');
Dog buddy = Dog('Buddy', 'Golden Retriever');

Access the variables set by the constructor:

print(rex.name);
print(rex.breed);
print(buddy.name);
print(buddy.breed);

Output:

Rex
German Shepherd
Buddy
Golden Retriever

Dart also offers a shorthand constructor syntax using this.variableName directly in the parameter list:

class Dog {
  String name;
  String breed;

  // Shorthand: automatically assigns parameters to instance variables
  Dog(this.name, this.breed);
}

Both versions produce the same result. The shorthand is a popular Dart feature that removes the need to write this.name = name manually.

You can also add default parameter values:

class Cat {
  String name;
  int age;

  Cat(this.name, [this.age = 1]); // age defaults to 1
}

Cat fluffy = Cat('Fluffy', 3);
Cat whiskers = Cat('Whiskers'); // age defaults to 1

print('${fluffy.name} is ${fluffy.age}');    // Fluffy is 3
print('${whiskers.name} is ${whiskers.age}'); // Whiskers is 1

Key Point: Constructors ensure every object is properly initialized with data at the moment it is created, saving you from manually setting variables afterwards.

challenge icon

Challenge

Easy

You are given PHP files (book.dart and driver.dart) to implement a book system.

Your tasks:

  • Complete the Book class in book.dart by adding a constructor that initializes title, author, and pages — use the Dart shorthand syntax Book(this.title, this.author, this.pages);
  • In driver.dart, create a Book object for 'Harry Potter' by 'J.K. Rowling' with 400 pages

Follow the TODO comments in each file.

Cheat sheet

A constructor is a special method that runs automatically when you create a new object. It initializes the object's instance variables at the moment of creation.

The constructor has the same name as the class:

class Dog {
  String name;
  String breed;

  Dog(String name, String breed) {
    this.name = name;
    this.breed = breed;
  }
}

Create objects by passing arguments to the constructor:

Dog rex = Dog('Rex', 'German Shepherd');

Dart offers a shorthand constructor syntax using this.variableName directly in the parameter list:

class Dog {
  String name;
  String breed;

  Dog(this.name, this.breed);
}

You can add default parameter values using optional positional parameters:

class Cat {
  String name;
  int age;

  Cat(this.name, [this.age = 1]); // age defaults to 1
}

Cat fluffy = Cat('Fluffy', 3);
Cat whiskers = Cat('Whiskers'); // age defaults to 1

Try it yourself

import 'book.dart';

void main() {
  // TODO: Create a Book object with:
  // title: 'Harry Potter', author: 'J.K. Rowling', pages: 400
  Book myBook = ?;

  print("'${myBook.title}' by ${myBook.author}, ${myBook.pages} pages");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming