-1

For example object1 has an instance method1:

public void method1() {
    this.object2.method2(this);
}

Would the first 'this' refer to the object 1 and the second 'this' object 2 ?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mahamed Ducale
  • 119
  • 1
  • 8

1 Answers1

0

In your code the this keyword give the method a sense of which variable, object or instance it will call, while this can also call the parents variable, object, instance or method. So when it calls this.object2.method2(this) it actually calling for the object2's method2 with a parameter of the class. In your scope there is a code that instantiate the object2 and it is declared outside any method of the class. See more in here: use of this (javadocs)

Francis G
  • 1,040
  • 1
  • 7
  • 21