I am a beginner in java coding and I am having a hard time understanding the below scenario. Below is the screen shot of eclipse, while I was trying to debug my program. I could not understand why the variables section in the top right corner shows this
as a variable name and the value of this
as Test(class name)
. I always thought this is a pointer and not a variable. And moreover, the object for class test is not yet created. So, what is it referencing to? if you see the screenshot this is not used in the program. Still while debugging this is shown as a variable name
can anyone help me with this ?
From section 15.8.3 of the JLS:
When used as a primary expression, the keyword
this
denotes a value that is a reference to the object for which the instance method or default method was invoked (ยง15.12), or to the object being constructed.
So you can sort of regard it as a read-only variable, basically. You can do the same thing with it that you can do as most other variables.
In your screen-shot, your execution is at the start of a constructor - so this
refers to the object currently being initialized.
See more on this question at Stackoverflow