wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes serialization question - pls help me! 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 question - pls help me!" Watch "serialization question - pls help me!" New topic

serialization question - pls help me!

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
I dont understand the effect of serialization. What can it actually contribute to the following objects and code?

i just cant see it!

i tried to test the effect by altering the code but dont know where to start...coz i am not quite sure (still) what serialization is ALL about!

K&B added in order to alter the standard deserialization process you would override the readObject() method in SpecialSerial. Why???

and most importantly, if serialization is about saving objects (instance variables), line 9's ++s.z is outside os.close(),
how come on line 14, the value changed?

====here is the code======
import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
System.out.println(++s.z + " "); //line 9

ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
System.out.println(s2.y + " " + s2.z); //line 14
} catch (Exception x)
{System.out.println("exc"); }

class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
void readObject(){ };
[ September 17, 2007: Message edited by: adam lui ]
Ajay Chandravadiya

Joined: Aug 06, 2007
Posts: 27
Please dont mind , but you need to do your homework here.

1. its "private void readObject(Objectlnputstream os) {} " that is to be overridden and NOT readObject(); // No Params?!
2. Serialization is NOT for Transients. All transients will be given Default value of its datatype when object is deserialized.
3. Serialization is NOT for Statics. All Static variables, even though when referred thru Object Reference, fetches its value from Class (and not Object).

Please let me know if I can still help.

SCJP Tiger Hunter 91%.
Roland Schneider

Joined: Sep 19, 2007
Posts: 12
1. No readObject(...) Method can be overriden, the method must be implemented in any class which needs special treatment in the deserialization process.

2. Static members should be accessed in a way like SpecialSerial.z and then it's very easy to see why static member vars are not serialized.

[ September 19, 2007: Message edited by: Roland Schenner ]
[ September 19, 2007: Message edited by: Roland Schenner ]
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Please refer below link. You will clear.

SCJP-1.5<br />SCWCD-1.4
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy "Ajay C" !

Thanks for your contribution to this forum.

The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:

So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.

I'm posting this because I am one of the moderators of this forum.


all events occur in real time
I agree. Here's the link: http://aspose.com/file-tools
subject: serialization question - pls help me!
It's not a secret anymore!