File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization for statics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization for statics" Watch "Serialization for statics" New topic
Author

Serialization for statics

Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Holla everyone,

This question is from oracle website (sample questions).


output is 7 7 0


but according to the answers given in the website its 0 7 0.
can any one please tell me what is wrong with this code ? i know that static variables cannot be serialized.
you can check the question Here

Thanks a lott
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Like you say, statics aren't serialized. However.... This code is launched during one instance of a JVM. You set the static variable to 7, and the reason it prints 7 after deserialization is because it is still 7. But it has nothing to do with the deserialization, it's just that the value hasn't changed since you serialized your object t.

Hope that was clear...

EDIT: As for the answer according to the website, I assume its an error.

// Andreas
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Hi, Chaitanya Kidambi

You are right, static variable or class variable has nothing to do with Serialization and Desirialization. I liitle modified your code, see here

Output : 8 7 0
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
hi Aashu,

You are right.You give the crystal clear soluntion.


Cheers Munees
My Blog
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Andreas Svenkson wrote:

EDIT: As for the answer according to the website, I assume its an error.

// Andreas

Hello there, yeah it might be an error. thanks for the reply.
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Aashu Mahajan wrote:Hi, Chaitanya Kidambi


Output : 8 7 0


Hello Aashu,
thanks for the reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization for statics