0

Being a newbie, I'm trying to understand serialization and I don't get why one would need to reassign static fields after deserialization (as some suggest in order to keep original values) if by definition all objects of the same class have the same values of static parameters. Isn't it true that a new object automatically receives all static parameters of its class? I mean if deserialization nullifies object's static members and we keep the original object wouldn't that mean that there will be two objects of the same class with different values of static parameters? How's that possible or how am I wrong?

EDIT: It seems that I wasn't clear enough and I'm sorry about that.

When writing my first sentence I was keeping in mind Bruce Eckel's "Thinking in Java" (4th Ed.). I'm not sure if I can attach a few scans of his book without violating copyright but I actually stumbled upon the last example in "Object serialization" chapter (pp.715-718). By means of his io/StoreCADState.java and io/RecoverCADState.java Eckel gets objects of the same class with different static values. He doesn't go deep enough for me into explanation and I feel totally confused.

I know that static members belong to a class not an object. Although I'm not sure now what it means precisely. I used to believe it implied that whenever an object is created a set of static parameters is 'taken' from the class description and 'added' to an object. If this is correct then why they are not 'added' to an object after deserialization when Object instance being cast to a specific class?

Vic
  • 211
  • 2
  • 9
  • Serialization/deserialization is an extralinguistic mechanism for creating Java objects without utilizing the class constructor. There are many caveats and cautions to be aware of when using the serialization facility. But, just as you can construct objects using the `new` operator without passing static fields as parameters to the constructor, you can also create objects by deserializing a data stream without that stream containing static fields. – scottb Apr 10 '16 at 18:38

3 Answers3

1

Have a look at the Javadoc for static members here. This is what it says:

They are associated with the class, rather than with any object.

As static members are never associated with any object, they are never stored while serializing the object. Also, new object won't automatically receive all static parameters as they have nothing to do with serialization-deserialization of the objects. If you are trying to store static variables with objects then I'd say it's a design flaw. We should rather declare them as non static if we want to persist their values with objects.

Update:

If you want to store non serializable fields with object then you can do so by writing your implementation of readObject() and writeObject() methods. Have a look at the answer of this SO question for example.

Community
  • 1
  • 1
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1

How does serialization reconcile static members?

It doesn't.

I don't get why one would need to reassign static fields after deserialization

You don't.

(as some suggest in order to keep original values)

They're wrong.

if by definition all objects of the same class have the same values of static parameters.

Objects don't have values of static parameters at all. The static members are in the class, not in the object.

Isn't it true that a new object automatically receives all static parameters of its class?

No.

I mean if deserialization nullifies object's static members

It doesn't.

and we keep the original object wouldn't that mean that there will be two objects of the same class with different values of static parameters?

No, because it doesn't happen.

How's that possible

It isn't.

or how am I wrong?

Several ways. Serialization does precisely nothing with static members.

EDIT

It seems that I wasn't clear enough and I'm sorry about that.

You were clear enough. You're just fundamentally mistaken about several things.

When writing my first sentence I was keeping in mind Bruce Eckel's "Thinking in Java" (4th Ed.). I'm not sure if I can attach a few scans of his book without violating copyright

You can't (shouldn't) post images of text here, but you can quote a bit of the book under 'fair use' provisions.

but I actually stumbled upon the last example in "Object serialization" chapter (pp.715-718). By means of his io/StoreCADState.java and io/RecoverCADState.java Eckel gets objects of the same class with different static values.

Impossible. You must have misunderstood. Or else he is using different class loaders, which is a complication not mentioned in your question.

He doesn't go deep enough for me into explanation and I feel totally confused. I know that static members belong to a class not an object. Although I'm not sure now what it means precisely. I used to believe it implied that whenever an object is created a set of static parameters is 'taken' from the class description and 'added' to an object.

No. The static data stays where it is, in the class. If there were multiple copies of it, it couldn't possibly work, or be describable as 'static'.

If this is correct

It isn't.

then why they are not 'added' to an object after deserialization

Because Serialization doesn't deal with static objects in any way.

when Object instance being cast to a specific class?

And that process takes place after Serialization, not during it, and doesn't have anything to do with static data either.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Because static members belong to the class, not to the object, they're not serialized or deserialized by default.

A Class with the same name and different values in static members can be only in another namespace (loaded by another ClassLoader).

Read this to understand: How to serialize static data members of a Java class?

Community
  • 1
  • 1