0
Can anyone please explain about "this" in java.
2 Answers
+ 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;
}