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.
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.