0

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

Eclipse screenshot

can anyone help me with this ?

1

3 Answers 3

5

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.

2
  • thank you. so in this scenario should this be considered as a variable? if so what is the value of this? also if there is a statement inside the constructor throwing an exception, will the object be created at all? if yes what will the value of object be?
    – angel
    Feb 9, 2016 at 4:45
  • @angel: It's not quite a variable in the normal sense, but the value of it is a reference to the object for which the instance method or default method was invoked, or to the object being constructed - exactly as per the spec I quoted. And yes, an object is created even if the constructor throws an exception... think of a constructor as initialization after the object has been created in memory. (Depending on what happens elsewhere in the constructor, the object could even still be used later, e.g. if you have someStaticField = this; before the exception is thrown.
    – Jon Skeet
    Feb 9, 2016 at 7:00
0

this represents the current object in the current context. In any given instance context (method), this represents the object on which this method was called.

Note that for any call of an instance method the object (this) is implicitly passed. So, you see the instance of this in the variable section.

this is not exactly a reference (technically it actually points to the current object).

0

A pointer is a variable. It is a variable that contains a memory address for the object being referenced.

In you debug window, note the "(ID=18)". This is an internal ID for the individual object. So your object has been instantiated, and the this keyword is currently referring to the instance of that object with ID 18.

If your debugger is on this file, then the object has been allocated. If I remember my Eclipse debugging correctly, it looks like the constructor is being called. This means that all static items have been evaluated, and your object is ready to execute it's constructor - meaning it's already been allocated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.