Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign an object to null [duplicate]

Tags:

java

If I instantiate an object and pass it to an function, in this function I assign this object to null. It seems when return from the function, the object still there.

I just want to know when I assign null, what happens.

like image 897
shirley Avatar asked Dec 20 '22 12:12

shirley


1 Answers

You can never assign to an object. All you ever have are primitives and references. A reference is either null or a pointer to an object of suitable class.

Java arguments are passed by value. Your called method got a copy of a reference. It made that reference null. The calling method has its own reference which was unaffected by any assignments to the passed copy. That reference still points to the object.

like image 160
Patricia Shanahan Avatar answered Jan 03 '23 02:01

Patricia Shanahan