Instance variable is a variable which declared inside the class but outside any function.
Instance method is a method which declared inside the class but outside any function.
In below example we can see that we have crated on class called Student and inside the call we have declare two variable which is " Id and name ",
This two variable called in instance variable.
While we create three function in this class which are study , sleep and Alldetails , this three function called Instance Method.
For calling the class we use object , here we have use student1 as object and we call this object to call class variable and method to get output.
void main() {
//Using object to call
var student1 = Student(); // One Object, student1 is reference variable
student1?.id = 01;
student1?.name = "Vinit";
student1?.Alldetails();
student1?.study();
student1?.sleep();
}
// Define states (properties) and behavior of a Student
class Student {
int? id ; // Instance Variable, default value is null
String? name; // Instance Variable, default value is null
//Instance Method
void study() {
print("${this.name} is now studying");
}
//Instance Method
void sleep() {
print("${this.name} is now sleeping");
}
//Instance Method
void Alldetails()
{
print("$id is $name");
}
}
ย