The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Related to Object Serialization 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 "Related to Object Serialization" Watch "Related to Object Serialization" New topic

Related to Object Serialization

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572

The following code prints "pcp" whereas i think it should print "pcpc". Can anybody explains why it prints "pcp"?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
How about you explaining why you think it prints that final "c"?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Ali,

i think it should print "pcpc"

First think about, what Barry said, but if you don't get it, see below the "spoiler".


The last c will not print, because in deserialization, the constructors of classes implementing Serializable will not run.
Only constructors of superclasses not implementing Serializable will run.

If you have the K&B book, you can look it up on pages 453 and the following.

[ October 06, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
I think that when it will read the object from serialized file, it will create new CardPlayer() and will then set the values, so if its true then again "pc" should be printed making "pcpc", but i know i am wrong

Can you explain why?
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Thanks BH, i got that
I agree. Here's the link:
subject: Related to Object Serialization
It's not a secret anymore!