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 ?
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 ?
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.
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.