-2
public class Temp {
  private int x = 3;

  public void show() {
    this.x = 4;
    this.show(); // same as show();
  }
}

Can we say that this is a reference variable ?

André Stannek
  • 7,773
  • 31
  • 52
Abhishek Dubey
  • 937
  • 8
  • 11

3 Answers3

2

From the Java Language Specification:

[...] the keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked [...]

So this is not a reference variable, it is in fact a keyword. From the above description you could say it behaves like a reference variable (or better said a constant since you can't change it) if it is used in a certain context.

André Stannek
  • 7,773
  • 31
  • 52
1

this is a reference indeed, however it is a constant, thus you can't change its value. this always references an object that object method was invoked on.

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Can someone call it a reference variable ?? – Abhishek Dubey Apr 19 '18 at 07:22
  • @AbhishekDubey Only a guy called Michał Tomaszewski from PJATK college. ; p But it is a definitely a reference just a constant one. I believe( didn't check it ) that it is implemented as local constant in scope of method as it would be the most natural way to do it. – Yoda Apr 19 '18 at 07:23
-1

Yes. it is in fact a reference variable which refers to the current object.

piy26
  • 1,574
  • 11
  • 21