Here is the serialize class
And this is the main method
Thanks for your help
Paul Clapham wrote:At line 11, you clear the list. (That part I guess is obvious to you.)
At line 12, you call the Reserect method, which deserializes from the file and assigns the result to a local variable inside the Reserect method. That has no effect on the variable in your "main" method.
If the Reserect method is supposed to be returning a list, then it should do that. There's also no point in passing it a list, because it isn't going to do anything with an existing list. It's going to produce a new one. So its signature should be like this:
Paul Clapham wrote:I think you have it right, but just to be clear, here's what's happening.
In your "main" method you create a List object and assign it to the variable "lur". Then you call some methods which populate that list with a User object. To be precise, what you now have is a List variable named "lur" which contains a reference to a List object which is stored in the heap somewhere. It's important to keep those two things (variable and object) separate, because if you don't then you get confused.
Now, when you call your "Reserect" method, you pass a copy of the "lur" variable to the method. Not a copy of the object, but a copy of the variable. So now inside the Reserect method you have a variable named "lu" (that's the parameter) which contains a reference to that same List object. Okay so far? We have two variables which both refer to the same List object.
Next, at line 25 inside Reserect you call "os.readObject()". This produces a new List object, which came from the file so it contains the data you originally serialized. And then you assign a reference to that new List object to the "lu" variable. So now the "lur" variable in the main method refers to the original List (the one which you cleared) and the "lu" variable in the Reserect method refers to the new List (the one which came from the file).
Finally you return from the Reserect method. Its local variables (including "lu") vanish because their scope was only inside Reserect. But back in the "main" method the variable "lur" is still there, still pointing at the original List. Not at the one which came from the file -- that one now has no variables referring to it any more.