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 static in 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 "static in Serialization" Watch "static in Serialization" New topic
Author

static in Serialization

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, this is from Exam Lab. I read that static variables do not participate in Serialization. But, here it participates in Serialization..Why ??
Colin Wang
Greenhorn

Joined: Nov 24, 2010
Posts: 4
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
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.


OCPJP6
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3829

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static in Serialization