File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Serialization Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Serialization Question" Watch "Serialization Question" New topic

Serialization Question

Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
I hope someone can explain the result I get from running the following code
Running this code I would expect the object m to be serialized with a counter value of 2. However when I run the following code I get the result of 0

Can someone please explain the result I am getting and how to resolve the issue.
Joe Ess

Joined: Oct 29, 2001
Posts: 9190

Static members of a class are not persisted. When you declare a member of a class to be "static", you are tying its state to the class rather than a particular instance of that class. Serialization is designed to persist the state of an instance, so it ignores static members (and members flagged with the "transient" keyword).
In your case, your Model class gets reloaded when the VM goes away and starts back up so the counter starts back at 0.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Serialization Question
It's not a secret anymore!