Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Serialization for statics

 
Ranch Hand
Posts: 47
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Aashu,

You are right.You give the crystal clear soluntion.
 
Chaitanya Kidambi
Ranch Hand
Posts: 47
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashu Mahajan wrote:Hi, Chaitanya Kidambi


Output : 8 7 0



Hello Aashu,
thanks for the reply.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic