• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

serializable problem

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


o/p

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
instead.
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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Regards,
Edwin Dalorzo
[ April 08, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic