In Dart, the key difference between an instance variable and a static variable is how they are associated with the class and its objects. An instance variable belongs to a specific object of the class. Each object has its own copy of that variable, so changing it in one object doesnβt affect others. For example, if I have a Car class with an instance variable color, each Car object can have a different color.
class Car {
String color; // instance variable
Car(this.color);
}
If I create two cars:
var car1 = Car('Red');
var car2 = Car('Blue');
car1.color is 'Red' and car2.color is 'Blue', completely independent.
On the other hand, a static variable belongs to the class itself, not to any object. All instances of the class share the same static variable. For example:
class Car {
static int totalCars = 0; // static variable
Car() {
totalCars++;
}
}
Every time a new Car object is created, totalCars increases, and all objects see the same value.
