I have a class
public final class GGGGG {
private final String str;
public GGGGG(final String str) {
this.str = str;
}
public void showElement(final String test){
System.out.println(this.str+test);
}
public static void main(String[] args) {
GGGGG hello = new GGGGG("hello");
final Test2 test2 = new Test2(hello::showElement);
test2.test();
hello = null;
test2.test();
}
static class Test2{
private final Consumer<String> consumer;
Test2(final Consumer<String> consumer) {
this.consumer = consumer;
}
public void test(){
this.consumer.accept(" world");
}
}
}
What I don't understand, in class GGGG I have String str
(state)
I create a consumer with method reference to the method showElement
And now this consumer has reference to the GGGGG
instance.
Does consumer keep a reference to the original object or create a new instance, if it the same reference when it will be garbage collected?