This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization SCJP KS && BB

 
Prashant Bhardwaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In chapter 6 --> Serialization, I came to learn that Static variables' are not saved as part of Object's state.Serialization Is Not for Statics.
Yet in the question no. 15 (last question) of the excercise :

The answer is 10 0 10.

My question is why did s2.z = 10 when it should've actually not retained the value incremented by s (going by the literature)?
I know that there is a gap in my understanding...please educate me.
 
christian combarel
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
When you write ++s.z,the compiler writes ++SpecialSerial.z, as z is static.
Then, when you write s2.z you still have SpecialSerial.z
z is not serialized, it stays in the SpecialSerial class it has nothing to do with s or s2.
 
Prashant Bhardwaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply.
I took the reading/ObjectInputStream part out of the given code and put it in a seperate file TestSer2.java.
Upon compiling and subsequent runing TestSer2,the value I got is 10 0 9.
So, it means that in the 1st case (TestSer.java), because SpecialSerial was still available in memory, Specialserial.z++ value was retained.
Not if we invoked it seperately.

Phew !, its so easy to overlook the details.
I'm beginning to feel nervous.
Somebody pls provide any tips to prepare for this exam!.
 
Dave Walsh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prashant,

Actually, there are not many details in this case, although I totally agree there are numerous details to know for SCJP certification and they make me nervous as well.

Here's the deal.
- For instance member y: transient fields are not serialized. So, when s is deserialized to create s2, s2's y is assigned int default value, 0.

- For class (static) member z: Christian is right. This is a class instance (static) so it has nothing to do with serialization. What is mind boggling is that they use instantiated objects to change its value. That's not nice at all.

Now, I would be interested to see the code you said produced 10 0 9. How did you get instance s2, with "new" ? Because if you don't deserialize s to get s2, then s2's "y" value should be 7 and z, of course, remains 10. Here's the code I have and the output is 10 7 10:



Finally, you can read my post here to see what I think about SCJP certification and how I intend to prepare for the exam.

Take it easy,

Dave

[ August 05, 2007: Message edited by: Dave Walsh ]
[ August 05, 2007: Message edited by: Dave Walsh ]
 
Prashant Bhardwaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I am saying buddy, is that if you take the reading serialized part out of the given code, and put it into a seperate file, you'l see that the incremented value of static z is not retained :



Compile and run this. You'l get a 0 9 output.
Note: This code assumes that TestSer2.java has been compiled and run already, and an instance of SpecialSerial has been serialized to the file named "myFile".

But anyways, thanks for your attempt to cheer me up.
Can't help being discouraged at times... the questions are sneaky.
but then I recover and restart. :-)
 
Dave Walsh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prashant Bhard:
what I am saying buddy, is that if you take the reading serialized part out of the given code, and put it into a seperate file, you'l see that the incremented value of static z is not retained :



Compile and run this. You'l get a 0 9 output.
Note: This code assumes that TestSer2.java has been compiled and run already, and an instance of SpecialSerial has been serialized to the file named "myFile".

But anyways, thanks for your attempt to cheer me up.
Can't help being discouraged at times... the questions are sneaky.
but then I recover and restart. :-)

Oh the output is 0 9, not 10 0 9 (see your 2nd post).

Now I see. You serialized an object in a previous program execution and attempted to deserialize it in a new execution. Then the result you have is normal.

In the code you just posted, z is never used and it expectedly keeps the value it was instantiated with. And you're right, the fact that it's been used in the 1st execution doesn't matter. The JVM loads fresh classes and their data when you (re)start a program.

Generally, when a program stops, what it was using in RAM dies. If you restart the program, or another one that uses the same class, then you have fresh data in RAM. This rule applies to any programming language.

D.
[ August 05, 2007: Message edited by: Dave Walsh ]
 
Manoj Macwan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Running the above program I got one warning message like:

warning: [serial] serializable class SpecialSerial has no definition of serialVersionUID

Can anyone throw some lights on this warning message?
 
Abhishek Kanchan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Macvan,
SerialUIDVersion is just a version number given to a class to ensure that the class has not changed its state since it has been serialized.If the version numbers are different for a serialized class and same class after deserialization then it throws runtime error.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic