It's "whatever object the method was called on". We can't tell where the object is created, because that's presumably in some other code.
Have a look at this simple, complete example:
class Person {
private final String name;
public Person(String name) {
// this refers to the object being constructed
this.name = name;
}
public void printName() {
// this refers to the object the method was called on
System.out.println("My name is " + this.name);
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Person("Jon");
Person p2 = new Person("Himanshu");
p1.printName(); // Prints "My name is Jon"
p2.printName(); // Prints "My name is Himanshu"
}
}
The first time we call printName()
, we call it on the object that p1
refers to - so that's the object that this
refers to within the method, and it prints the name Jon. The second time we call printName()
, we call it on the object that p2
refers to - so that's the object that this
refers to within the method, and it prints the name Himanshu.
(It's important to note that p1
and p2
aren't objects themselves - they're variables, and the values of the variables aren't objects either, but references. See this answer for more details about that difference.)