ArrayList initiated as null in a function and passed to another function. In another function I checked null and empty for that list and assigned newly created list if passed list is found null. However when I got back from that function, I found that list still be null. As java is passed by value, any changes made in passed list should be reflected in returned list
Asked
Active
Viewed 368 times
0
-
Please *show* your code rather than describing it. I suspect the problem is likely to be that you've misunderstood what's being passed in Java - it's not an object, it's a reference. The reference is passed by value, but modifications to the object will still be visible via the reference later. – Jon Skeet Sep 23 '16 at 06:57
-
Please add some code – Venkat Sep 23 '16 at 06:57
1 Answers
0
Java is, as you say, pass by value.
It passes the pointer to your list by value.
A method cannot update the variables used by its caller with new values (that would be pass by reference).
All you can do in your method is change the contents of the list that you have been passed (by invoking the appropriate methods on it). You cannot change it to another list. If you want to do that, you need to send back the new list as a return value.

Thilo
- 257,207
- 101
- 511
- 656