Menu
Coddy logo textTech

Instance Variables

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

Instance variables are variables declared inside a class. Each object created from the class gets its own copy of these variables, independent from all other objects.

Declare instance variables with a type and an optional default value:

class Student {
  String school = 'Dart Academy'; // has a default value
  String name = '';               // empty default
  int age = 0;                    // zero default
}

Variables with default values start with the same value for every new object. Variables like name and age start empty or zero until you set them.

Use a method with this to set individual object data:

class Student {
  String school = 'Dart Academy';
  String name = '';
  int age = 0;

  void setInfo(String name, int age) {
    this.name = name; // this.name = instance variable
    this.age = age;   // this.age  = instance variable
  }
}

Create students and assign their data:

Student student1 = Student();
Student student2 = Student();

student1.setInfo('Alice', 20);
student2.setInfo('Bob', 22);

Access instance variables using the dot operator:

print('${student1.name} is ${student1.age} at ${student1.school}');
print('${student2.name} is ${student2.age} at ${student2.school}');

Output:

Alice is 20 at Dart Academy
Bob is 22 at Dart Academy

Notice that school is the same for both objects because it has a default value, while name and age are unique to each object because they were set individually through setInfo.

challenge icon

Challenge

Easy

Complete the Student class in student.dart:

  1. Declare a String school instance variable with default value 'Dart Academy'
  2. Declare a String name instance variable with default value ''
  3. Declare an int age instance variable with default value 0
  4. Add a setInfo method that takes String name and int age as parameters and sets them on the object using this

The driver.dart file will test your class by creating two students and printing their information (locked).

Cheat sheet

Instance variables are variables declared inside a class. Each object gets its own copy of these variables.

Declare instance variables with a type and an optional default value:

class Student {
  String school = 'Dart Academy'; // has a default value
  String name = '';               // empty default
  int age = 0;                    // zero default
}

Use a method with this to set individual object data:

class Student {
  String school = 'Dart Academy';
  String name = '';
  int age = 0;

  void setInfo(String name, int age) {
    this.name = name; // this.name = instance variable
    this.age = age;   // this.age  = instance variable
  }
}

Create objects and assign their data:

Student student1 = Student();
Student student2 = Student();

student1.setInfo('Alice', 20);
student2.setInfo('Bob', 22);

Access instance variables using the dot operator:

print('${student1.name} is ${student1.age} at ${student1.school}');
// Output: Alice is 20 at Dart Academy

Try it yourself

import 'student.dart';

void main() {
  Student student1 = Student();
  Student student2 = Student();

  student1.setInfo('Alice', 20);
  student2.setInfo('Bob', 22);

  print('${student1.name} is ${student1.age} at ${student1.school}');
  print('${student2.name} is ${student2.age} at ${student2.school}');
}
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