• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization for statics

 
Chaitanya Kidambi
Ranch Hand
Posts: 47
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Aashu,

You are right.You give the crystal clear soluntion.
 
Chaitanya Kidambi
Ranch Hand
Posts: 47
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aashu Mahajan wrote:Hi, Chaitanya Kidambi


Output : 8 7 0


Hello Aashu,
thanks for the reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic