I dont understand .this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I dont understand .this

17th Nov 2017, 5:55 PM
Curly Fries
Curly Fries - avatar
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.
17th Nov 2017, 6:13 PM
AgentSmith
0
thanks
17th Nov 2017, 11:40 PM
Curly Fries
Curly Fries - avatar
0
but what difference does it make using or not using it
17th Nov 2017, 11:41 PM
Curly Fries
Curly Fries - avatar