Menu
Coddy logo textTech

Constant Constructors

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

A constant constructor creates compile-time constant objects. When you create objects with the same values using a const constructor, Dart reuses the same instance in memory, improving performance.

To create a constant constructor, add the const keyword before the constructor name, and ensure all instance variables are final:

class Color {
  final int red;
  final int green;
  final int blue;

  const Color(this.red, this.green, this.blue);
}

const white = Color(255, 255, 255);
const alsoWhite = Color(255, 255, 255);

print(identical(white, alsoWhite)); // true

Both white and alsoWhite point to the exact same object in memory because they were created with identical values using a const constructor.

You can also use const constructors without the const keyword, which creates regular (non-constant) instances:

var red = Color(255, 0, 0);       // Regular instance
const blue = Color(0, 0, 255);    // Constant instance

Constant constructors are commonly used for immutable configuration objects, predefined values like colors or sizes, and anywhere you want to guarantee object immutability while optimizing memory usage.

challenge icon

Challenge

Easy

Let's build a configuration system for a simple game that uses constant constructors to define immutable, reusable settings.

You'll create two files to organize your code:

  • difficulty.dart: Define a Difficulty class that represents game difficulty settings. Your class should have:
    • Three final fields: name (String), enemyCount (int), and playerLives (int)
    • A constant constructor that initializes all three fields
    • A display() method that prints the difficulty information
  • main.dart: Import your difficulty class and create three constant difficulty presets:
    • easy with name 'Easy', 5 enemies, and 5 lives
    • medium with name 'Medium', 10 enemies, and 3 lives
    • hard with name 'Hard', 20 enemies, and 1 life
    Create all three as compile-time constants using the const keyword. Then call display() on each in the order listed above. Finally, check if two const Difficulty('Easy', 5, 5) objects are identical using identical() and print the result.

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

[name]: [enemyCount] enemies, [playerLives] lives

Expected output:

Easy: 5 enemies, 5 lives
Medium: 10 enemies, 3 lives
Hard: 20 enemies, 1 lives
true

Cheat sheet

A constant constructor creates compile-time constant objects. When objects with identical values are created using a const constructor, Dart reuses the same instance in memory.

To create a constant constructor, add the const keyword before the constructor name and ensure all instance variables are final:

class Color {
  final int red;
  final int green;
  final int blue;

  const Color(this.red, this.green, this.blue);
}

Creating constant instances with identical values results in the same object in memory:

const white = Color(255, 255, 255);
const alsoWhite = Color(255, 255, 255);

print(identical(white, alsoWhite)); // true

Const constructors can also be used without the const keyword to create regular instances:

var red = Color(255, 0, 0);       // Regular instance
const blue = Color(0, 0, 255);    // Constant instance

Constant constructors are useful for immutable configuration objects, predefined values, and optimizing memory usage.

Try it yourself

import 'difficulty.dart';

void main() {
  // TODO: Create three constant difficulty presets using the const keyword:
  // - easy: name 'Easy', 5 enemies, 5 lives
  // - medium: name 'Medium', 10 enemies, 3 lives
  // - hard: name 'Hard', 20 enemies, 1 life
  
  // TODO: Call display() on each difficulty in order: easy, medium, hard
  
  // TODO: Check if two const Difficulty('Easy', 5, 5) objects are identical
  // using identical() and print the result
}
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