"this" keyword-1

void main() {
  
  
  
  // Significance of "this" keyword 
  
  
  var  employee1 =   Employee();  // Creating employee1 object   
  
  var  employee2 =   Employee(); // Creating employee2  object 
  
  
  
  //Attribute <=> Instance Variable <=> Field Variable 
  
  
   // Setting the instance variable of employee1 object 
   
  employee1.id =1;
  employee1.firstname ="John";
  employee1.lastname ="Doe";
  employee1.position = "CTO";
  employee1.number_of_day_worked=12;
  
  
  
  employee2.id =2;
  employee2.firstname ="Jane";
  employee2.lastname  ="Doe";
  employee2.position ="CEO";
  employee2.number_of_day_worked=10;
  
 // companyname for employee2 will be set to "A" , because we 
 // didn't set it, it take the default value from class 
  
  
  
  
   // Methods Calling <=> Invoke method 
  
  
  //
  
  
   employee1.calculate_salary();
  
   employee1.fullname();
  
   
  
   
  
   print("-----------------------");
  
  
  
  
   employee2.calculate_salary();
  
   employee2.fullname();
  
  
}



class Employee{
  
  
  int id;
  
  String firstname;
  
  String lastname;  
  
  String position;
  
  int number_of_day_worked;
  

  
  
  
  //Method 
 
  void team_meeting_schedule(){
    
    // logic for team_meeting 
    
   
   
    
  }
  
  
  
  void fullname(){
    
    //logic for full name 
    
    print("${this.firstname}"+" "+"${this.lastname}");
    
  }
    
  void calculate_salary(){
    
    // logic for calculating salary
    
    int per_day_income = 100;
    
    print("${per_day_income*(this.number_of_day_worked)}");  
    
  }
  
}

Think of "this" as a placeholder or a reference variable for an instance . When we invoke a method on an instance , "this" is replaced by that instance

More about "this" keyword in

Basic Parameterised Constructor & "this" keyword -2

Last updated