![]() |
VOOZH | about |
Keywords are the words in a language that are used to represent some predefined actions or some internal process. We use the this keyword when we want to introduce the current object for a class. Then using the dot operator (.), we can refer to instance variables, methods and constructors by using this keyword. this keyword is also used with auxiliary constructors.
Let's understand this keyword with some examples.
Example #1:
Output:
15 + 12 = 27
In above example, a class addition defined which consist one parameter and in that class we created a method using this keyword with two parameters i and j. In this method a primary constructor is also invoked (i.e this(i)).
Example #2:
Output:
Language name Scala published article 105
As we can see, in above example, an auxiliary constructor defined with this keyword and a primary constructor called using this keyword. instance variables (i.e Lname, Articles)is also referred using dot(.) operator.