+ 1
I dont understand .this
3 Answers
+ 6
'this' refers to the instance of the object.
So lets say I have a class and one of its members is a variable called "name." Now lets say that I create two instances of that class, one named objOne and the other objTwo. After creating the objects, I use one of them to call a method that's stored in the class called setName. In that method, I may have:
public class setName(String name){
this.name = name;
}
So when that particular object calls that method in the class, 'this.name' is referencing the instance of the object that called the method, rather than the class or all objects.
For example:
MyClass objOne = new MyClass();
objOne.setName("Netkos");
^In this case, this.name in the class code is referring to objOne.name, which is the instance of the class that called the method.
0
thanks
0
but what difference does it make using or not using it