Menu
Coddy logo textTech

Initializer Lists

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

An initializer list is the code that appears after a colon in a constructor, before the constructor body. It runs before the constructor body executes and is used to initialize instance variables.

You've already seen initializer lists in previous lessons:

Point.origin() : x = 0, y = 0;

The part after the colon (x = 0, y = 0) is the initializer list. Multiple initializations are separated by commas.

Initializer lists are essential when working with final variables, which must be set before the constructor body runs:

class Circle {
  final double radius;
  final double area;

  Circle(double r)
      : radius = r,
        area = 3.14159 * r * r;
}

Circle c = Circle(5);
print(c.radius); // 5.0
print(c.area);   // 78.53975

The initializer list can also perform calculations or transformations on constructor parameters before assigning them. This is useful when you need to derive values or validate data during object creation.

You can combine the shorthand this. syntax with an initializer list:

class Rectangle {
  final double width;
  final double height;
  final double area;

  Rectangle(this.width, this.height)
      : area = width * height;
}

Rectangle rect = Rectangle(4, 5);
print(rect.area); // 20.0

Here, width and height are assigned via shorthand, while area is calculated in the initializer list using those values.

challenge icon

Challenge

Easy

Let's build a box dimension calculator that uses initializer lists to compute derived values when objects are created.

You'll organize your code into two files:

  • box.dart: Define a Box class that represents a 3D box with dimensions and automatically calculated properties. Your class should have:
    • Three final double fields: length, width, and height
    • Two computed final double fields: volume and surfaceArea
    • A constructor that takes length, width, and height using the this. shorthand, and uses an initializer list to calculate volume (length × width × height) and surfaceArea (2 × (length×width + width×height + height×length))
    • A display() method that prints the box information
  • main.dart: Import your box class and create two boxes:
    • A box with dimensions 3.0, 4.0, 5.0
    • A box with dimensions 2.0, 2.0, 2.0
    Call display() on each box in the order listed.

The display() method should print in this exact format:

Box: [length]x[width]x[height] | Volume: [volume] | Surface: [surfaceArea]

Expected output:

Box: 3.0x4.0x5.0 | Volume: 60.0 | Surface: 94.0
Box: 2.0x2.0x2.0 | Volume: 8.0 | Surface: 24.0

Cheat sheet

An initializer list is code that appears after a colon in a constructor, before the constructor body. It runs before the constructor body executes and is used to initialize instance variables.

Basic syntax with multiple initializations separated by commas:

Point.origin() : x = 0, y = 0;

Initializer lists are essential for final variables, which must be set before the constructor body runs:

class Circle {
  final double radius;
  final double area;

  Circle(double r)
      : radius = r,
        area = 3.14159 * r * r;
}

You can combine the shorthand this. syntax with an initializer list to assign some fields directly and calculate others:

class Rectangle {
  final double width;
  final double height;
  final double area;

  Rectangle(this.width, this.height)
      : area = width * height;
}

The initializer list can perform calculations or transformations on constructor parameters before assigning them, useful for deriving values during object creation.

Try it yourself

import 'box.dart';

void main() {
  // TODO: Create a box with dimensions 3.0, 4.0, 5.0
  
  // TODO: Create a box with dimensions 2.0, 2.0, 2.0
  
  // TODO: Call display() on each box in order
  
}
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