Dart: This Keyword

Dart: This Keyword

ยท

1 min read

  • In dart there are various keyword , and each keyword has their own meaning , The same for " this " keyword.

  • This keyword is use for determine different between the local variable and global variable of same variable name.

  • Global variable which are use by anyone while Local variable only use in particular function.

In below I am writing one example which display use of this keyword.

void main()
{
  var keywordThis = thisKeyword();
  keywordThis..printFunction(100 , 200);  
}

class thisKeyword
{
  int x = 15; //Global Variable
  int y = 20; //Global Variable

  void printFunction(int x , int y)  // Local Variable

  {
    //Without Using this keyword
    print("The Value of X : $x and The value of Y : $y"); 

    x = this.x;
    y = this.y;
    //Using this keyword
    print("The Value of X : $x and The value of Y : $y"); 

    //we can also write this type using this keyword
    //print("The Value of X : ${this.x} and The value of Y : ${this.y}"); 




  }
}

ย