Instance variables
Here’s how you declare instance variables:
class Point {
num x; // Declare instance variable x, initially null.
num y; // Declare y, initially null.
num z = 0; // Declare z, initially 0.
}
All uninitialized instance variables have the value null
.
All instance variables generate an implicit getter method. Non-finalinstance variables also generate an implicit setter method. For details,see Getters and setters.
class Point {
num x;
num y;
}
void main() {
var point = Point();
point.x = 4; // Use the setter method for x.
assert(point.x == 4); // Use the getter method for x.
assert(point.y == null); // Values default to null.
}
If you initialize an instance variable where it is declared (instead ofin a constructor or method), the value is set when the instance iscreated, which is before the constructor and its initializer listexecute.