In Java, "this" refers to the current object. I assumed that "this" is the same type as the current object, but consider this example:
class A {
static void f() {
System.out.println("A.f");
}
void g() {
this.f();
}
}
class B extends A {
static void f() {
System.out.println("B.f");
}
}
public class C {
public static void main(String[] args) {
B test = new B();
h(test);
}
static void h(B x) {
x.g();
}
}
The result is:
A.f.
Which I don't understand, because when x.g() is called, x is of type B. In the x.g() call, g is looked up in B, then in A (because B subclasses A). g then calls f, an instance method of both A and B, meaning that the version of f called depends on the type of the implicit THIS parameter. I would assume that B.f() would be called since X is of type B, but this is not so.
What type does THIS take on, exactly?