can someone please explain the use of "this" i've been seeing it a lot in getters and setters but its not fully clicking with me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can someone please explain the use of "this" i've been seeing it a lot in getters and setters but its not fully clicking with me

24th Nov 2021, 4:44 AM
Warlock Kadash
Warlock Kadash - avatar
2 Answers
+ 4
Specify the language you're asking. Generally, it's a pointer or reference to the current object. For example consider a class Test and a method print(). When you call obj.print(), the compiler automatically passes an extra argument with the function call which is called "this" pointer or reference(based on the language, pointer in C++). When calling the function, you know the object you're working with, but how do the function know which object to act on? To solve this, the compiler passes "this" to specify the object to act on. "this" is just a name chosen by the developer community for convenience. Feel free to ask if you got any more doubts on "this" Below is an example for demonstrating "this" https://code.sololearn.com/cdGyM55jE9g7/?ref=app
24th Nov 2021, 5:39 AM
Rishi
Rishi - avatar
0
Please always tag the language you're asking about. https://code.sololearn.com/W3uiji9X28C1/?ref=app
24th Nov 2021, 7:11 PM
Simon Sauter
Simon Sauter - avatar