-1

Please see my code below. I have a functional interface IFace with a method. I'm creating an implementation using Method reference from a class instance of Test. Can anyone tell me how the interface still refers to the instance method even if the reference is nullified? or reference is changed...?

public class Test {
    private int intVar;

    public Test() {
        intVar = 123;
    }

    public Test(int intVar) {
        this.intVar = intVar;
    }

    public void myInstanceMethod() {
        System.out.println("Hello from instance: " + intVar);
    }

    public static void main(String[] args) {
        Test t = new Test();

        IFace i = t::myInstanceMethod;
        i.method();  // Hello from instance: 123

        t = null; // Nullifying the reference
        i.method(); //  Hello from instance: 123
                    // Why still executing the method if reference is nullified?????

        t = new Test(456);
        i.method(); // Hello from instance: 123
                    // Why not Hello from instance: 456 ??????
    }

    static interface IFace {
        void method();
    }
}
Coder
  • 54
  • 6
  • 1
    you can check whether `t==i` (points to the same address space), if not then all good if it is then it is an issue, happy learning. Also note: Object address space is different from the reference variable's address space. – dkb Apr 26 '20 at 06:11
  • 2
    `t = null;` does not affect i. – samabcde Apr 26 '20 at 06:15
  • String representation of t and i com.test.javaadvanced.Test@1218025c com.test.javaadvanced.Test$$Lambda$1/531885035@816f27d – Coder Apr 26 '20 at 06:18
  • `t = null;` or `t = new Test(456);` are never used again in your code. You use `i` which has been assigned a captured value. – Naman Apr 26 '20 at 06:20

2 Answers2

0

In a sense, the question is similar to asking why printing b does not output null in the following program:

String a = "abc";
String b = a;
a = null;

System.out.println(b);

When IFace i = t::myInstanceMethod; finishes running, i has captured a reference to the object that t points to at this time. That reference is independent from the variable t. Look at it like i, the method reference, has its own variable pointing to the same object.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • How to debug the code in this case? If it's copying/cloning the object, does the method reference break the singleton feature? – Coder Apr 26 '20 at 06:22
  • @Coder not sure what you mean. There's only one thing you need to understand for your question to be answered: it's how objects and variables are managed in Java. When an object is created (like `new Test()`), there can be one or more variables pointing to it (like `t` or a variable managed in `i`). If one of these variables is reassigned to a different object, that doesn't get done for all the variables that were initially pointing to the same object. – ernest_k Apr 26 '20 at 06:26
0

The memory is not freed when you nullified the reference. It's freed when ALL references are gone. By passing method reference you created new reference to object itself, so the ref count is 2 at that point, i.e. the i holds reference to t underneath

Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24