It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes serializable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "serializable problem" Watch "serializable problem" New topic

serializable problem

Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
An Object that needs to be serialized must only contain primitive variables
or Serializable objects ,unless the fields are marked transient or static
I have a problem about deserialize ,when deserialize ,these fields which are maked transient or static if are initialize to default variable
I have demo it in the following
server code

client code


I found static objects field and static primitive type field are different ,if static fields are set to default value when deserialize
why do not oupput
maybe I need to test the server code and client code in different computer
and then get output as i expected
more what is the diference between static transient variable and transient variable
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Remember that the static member values are set when the class is loaded. Hence, every time your deserialize your objects, as your class has already been loaded their former values are not overriden.

Test your program by running it twice. The first time just serialize the object. The second time deserialize it. But before doing so, remove all default initializations on static and transient members. You better set those values prior to serialization.

The second time you run your program, load the object from the file. This time you will the the static content assumes the default values.

For instance....

This is the class to be seriliazed. Note as I leave static members without initialization. This is just to demonstrate the theory of serialization.

Now, the first time you run your test, do it like this:

And, the second time that you run your code. Do it like this;

The output should be "I am gun with 0 bullets" despite of setting the bullets to 10 when you serialized the object during the first test.

You will see similar results with transient variables.

Edwin Dalorzo
[ April 08, 2006: Message edited by: Edwin Dalorzo ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Changchun,

I don't know whether or not you have the K&B SCJP Study Guide for Java 5 Book.

If so, take a loot at Chapter 6, page 456. You will get some more details about serialization with static variables problem.

For those who don't have purchased the book yet, here's the great and simple explanation about the problems when trying to serialize static variables.

Serialization Is Not for Statics
Should static variables be saved as part of the object's state? Isn't the state of a static variable at the time an object was serialized important? Yes and no.

It might be important, but it isn't part of the instance's state at all.
Remember, you should think of static variables purely as CLASS variables.
They have nothing to do with individual instances. But serialization applies only to OBJECTS. And what happens if you deserialize three different Dog instances, all of which were serialized at different times, and all of which were saved when the value of a static variable in class Dog was different. Which instance would "win"? Which instance's static
value would be used to replace the one currently in the one and only Dog class that's currently loaded? See the problem?

Static variables are NEVER saved as part of the object's state because they do not belong to the object!

Hope that helps.

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
thanks Edwin
when deserialize ,these fields which are maked static are initialize to
the value according to static initializers and static block

but the fields which are marked transient are initialize to default value
[ April 08, 2006: Message edited by: Changchun Wang ]
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
I have not get this book yet
because I am from china,and in china I can not buy the book

thanks your reply!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
That's correct, comrade!

I do not know if some other more advanced topics related to serialization are part of the SCJP now.

One of this topics is the custom formatting of serialization process. If you implement two methods in the Serializable object you can override the default serialization process to accomodate specific requirements.

Those methods are private void writeObject(ObjectOutputStream) and private void readObject(ObjectInputStream).

Hence, you could actually save the state of the static member of the class Gun, that I wrote previously using this code.

If you test this new implmentation you will find that the serialization process of the current object state is overriden by the code programmed in the new private methods.

By means of this methods (which should be private) you can control the way an object serializes its state. For example, saving the state of the static members or data related to static or transient members.

For instance, database connection object should be transient, because it is not serializable. But you could serialize the connection string, so that when you deserialize the object you can restablish the connection with the database.

I guess, there a couple of other nices features related to serialization that may not be in exam.

But I hope you may find this interesting.

[ April 09, 2006: Message edited by: Edwin Dalorzo ]
[ April 09, 2006: Message edited by: Edwin Dalorzo ]
I agree. Here's the link:
subject: serializable problem
It's not a secret anymore!