Does myObject::someMethod strongly reference myObject? For some background, consider these two classes:
class TakesRunnable {
private Runnable runner;
TakesRunnable(Runnable runner) {
this.runner = runner;
}
public void run() {
runner.run();
}
}
class Foo {
public void runMe() {
...
}
}
If I were to instantiate a Foo object, create new TakesRunnable(foo::runMe), and then lose my local reference to foo, would foo be garbage-collected underneath me or would my TakesRunnable object maintain a strong reference to it? I would imagine the latter but that’s because I spend a lot of time in Python.
>Solution :
Yes, instance-method references maintain a reference to the object they relate to.
So in your case, the object foo will remain uncollectable until that method reference goes out of scope.