0

Can anyone please explain about "this" in java.

16th Nov 2017, 12:46 PM
Vivek
Vivek - avatar
2 Answers
16th Nov 2017, 1:07 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
'this' keyword refers to the instance variables. Simple example: public class Person { String name; int age; /* As you can see below, the parameters of the constructor are with the same names as the instance variables */ Person(String name, int age) { this.name = name; this.age = age; } If you want to ommit the 'this' keyword, you should use parameters with different names. Person(String newName, int newAge) { name = newName; age = newAge; }
16th Nov 2017, 1:10 PM
Boris Batinkov
Boris Batinkov - avatar