Hello @Estiak
Thanks for reaching out to us.
The main motto of using this keyword is to differentiate the formal parameter and data members of the class. If in case, the formal parameter and data members of the class are the same, then it leads to ambiguity. So, in order to differentiate between the formal parameter and the data member of the class, the data member of the class must be preceded by the “this” keyword.
In our code example, userName
is the class variable and declare in function too that’s why we used this keyword with a userName
that represents the current instance of a class. It is used to differentiate variables of the class (this. username) and formal parameters of the method/function(username).
Hope it will help, Happy Learning