0

I have a function which returns a reference to an object. Why cannot I assign this returned reference to point to another object? The reference being returned by the function is not final , hence I should be allowed to change it's value to point to another object.

class TestClass3 {
    public TestClass3 hello() {
        TestClass3 t = new TestClass3();
        return t;
    }
}

class TestClass1 {
    public static void main(String[] args) {
        TestClass3 obj = new TestClass3();

        // The below line of code gives an error
        obj.hello() = null; 
    }
}

I expect that the reference returned by calling the hello() method should be assigned null value.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Raj
  • 1
  • 1
  • You are trying to set a method to a value. This isnt possible and doesnt make sence. – Patrick Oct 14 '19 at 18:05
  • Your expected result makes no sense to me. _You_ don't actually manipulate an object in memory when doing `someVar = null`, the object stays where it is and the Garbage Collector will clean it up. What's why it makes no sense to "set a reference to null", Java doesn't provide such functionalities. You can set a variable to null to not use a certain reference anymore. – Tom Oct 14 '19 at 18:09
  • @Tom Yes it does throw an error, already by the compiler. Test it. The problem is: You can only assign values to variables. He wants to assign a value to a value (the reference counts a value, it isn't a variable) and that isn't possible – Patrick Oct 14 '19 at 18:33
  • @Patrick Where did I claim it doesn't raise a compiler error? And I don't need to test it, I know how Java works. Also thanks for explaining to me what I already explained to OP. – Tom Oct 14 '19 at 18:41

5 Answers5

1

You'll need to assign obj.hello() to a variable, which you can then set to null if you want.

Tanja Stroble
  • 51
  • 1
  • 4
0

You are trying to set the method to a value, this isn't possible and doesn't make any sense, if you think about it. If you want to save the reference returned by obj.hello(), write

TestClass3 obj2 = obj.hello();
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
Patrick
  • 552
  • 5
  • 17
0

Here in your code when you write obj.hello(), it only call the method hello. It doesn't assign the return of hello to any variable. So no variable is actually holding the return value of the method hello. While you can assign null value to same variable. That's why it's not a valid syntax.

Valid syntax should be like:

TestClass3 value = obj.hello();
value = null;
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

Simply speaking, it's because Java doesn't allow it while there are languages that do (e.g Python). In Java you cannot monkey-patch existing code.

Why? Well, Java wasn't born as a functional language, so while considerable for function references was added in Java 8, they're still not first class citizens. So you cannot assign a new value to a method of a class like you can with a field of that class.

However, since you can assign values to fields, you could do something like this:

class TestClass3 {

    public Supplier<TestClass3> hello;

    public TestClass3() {
        this.hello = this::hello;
    }

    public TestClass3 hello() {
        TestClass3 t = new TestClass3();
        return t;
    }
}

class TestClass1 {
    public static void main(String[] args) {
        TestClass3 obj = new TestClass3();
        obj.hello.get(); // calls hello()
        obj.hello = null; // now works
    }
}

Here I define a member named hello in TestClass3. It's of type Supplier<TestClass3>, and it just so happens that public TestClass3 hello() is of the same type. Hence, in the constructor of TestClass3 I can do this: this.hello = this::hello;.

Now we have a reference to hello() as a field, which means that we can invoke the function using that field, or even change the field's value:

obj.hello.get();
obj.hello = null;
Malt
  • 28,965
  • 9
  • 65
  • 105
0

obj.hello() is an object's behavior, not a reference, only reference can be set to null.

About null:

  • null is just a reference value.
  • reference is a variable that has a name and can be used to access an object by referring to the memory address where it is located.

About Object:

All objects have three essential features:

  • state
  • behavior
  • identity