my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization SCJP KS && BB 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 "Serialization SCJP KS && BB" Watch "Serialization SCJP KS && BB" New topic
Author

Serialization SCJP KS && BB

Prashant Bhardwaj
Greenhorn

Joined: Jul 25, 2006
Posts: 16
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

Joined: Aug 04, 2007
Posts: 47
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.



-------------
Chris
Prashant Bhardwaj
Greenhorn

Joined: Jul 25, 2006
Posts: 16
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

Joined: Jun 21, 2007
Posts: 24
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

Joined: Jul 25, 2006
Posts: 16
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

Joined: Jun 21, 2007
Posts: 24
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

Joined: Aug 03, 2007
Posts: 24
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

Joined: May 10, 2007
Posts: 32
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.


SCJP 1.5<br />SCWCD 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization SCJP KS && BB
 
Similar Threads
Serialization
question about serializable
Why static variable serialized here?
serialization
what happens when try to serialize static variable