• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static in Serialization

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this is from Exam Lab. I read that static variables do not participate in Serialization. But, here it participates in Serialization..Why ??
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:Hi, this is from Exam Lab. I read that static variables do not participate in Serialization. But, here it participates in Serialization..Why ??


"Primitives as well as non-transient, non-static referenced objects are encoded into the stream. " from wikipedia
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:Hi, this is from Exam Lab. I read that static variables do not participate in Serialization. But, here it participates in Serialization..Why ??



If you set b to something else, right before you deserialize, you will notice that it doesn't change -- it is still that something else. The value is two, not because of the deserialization, but because that is the value. Remember, there is only one copy of static variables, and if you don't change it, it won't change.

Henry
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, as told by Henry - the value is not because of deserialization, but because of the value of the static variable which was created while initializing the first object ob1.
You will notice the difference if you write serializing and deserializing code in two different files.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trivikram Kamat wrote:...You will notice the difference if you write serializing and deserializing code in two different files.


Or to see what happens: Just run the class file once and then comment the lines for serialization, compile, and run it again then it will use the previously serialized file as the source and inside a different JVM.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic