This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in Serialization" Watch "Problem in Serialization" New topic
Author

Problem in Serialization

Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121


Output is:
15


Static variables are Class variables and hence not serializable. But in the above program static instance t gets serialized. How?


OCPJP 6 - 96%
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You are serializing and deserializing in the same program (same JVM) so the static variable will be shared among all instances in that program. Run the serialization and deserialization as separate programs.
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1020
    
  16

Serialization topic were removed from OCPJP 6


Formely Pawel Pawlowicz
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Pawel Pawlowicz wrote:Serialization topic were removed from OCPJP 6


I saw a lot of questions on serialization on CertPal so I thought why not prepare it, as it can pop up on exam for few exam centers.

E Armitage wrote:You are serializing and deserializing in the same program (same JVM) so the static variable will be shared among all instances in that program. Run the serialization and deserialization as separate programs.


For that reason to check whether it's serialized or not, before serialization I changed t.i to 15 and after serialization I changed t.i to 17 and after deserialization t.i = 15 is returned, that means t is serialized as it returned the saved state of "i" when it was serialized. And running as separate programs also produces the same output, 15. Hence, it's serialized.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Post what you tried and make sure you understand what static means. All objects of the class in the same value will always share it.
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

E Armitage wrote:Post what you tried and make sure you understand what static means. All objects of the class in the same value will always share it.





I think I know what static means, though I can't understand what do you mean by this.
E Armitage wrote:All objects of the class in the same value will always share it.

E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
But you are checking the value of i and i is NOT static!

Also, what does the class A have to do with any of this?
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

E Armitage wrote:But you are checking the value of i and i is NOT static!

Also, what does the class A have to do with any of this?


If t would not have been serialized, then the output would have been 11, default value of "i". But t here is serialized even though it's static, and output is 15.
Doing deserialization via different program ensure JVM does not remember the state of "i"(as you said in your previous post) but still it returns 15, so static "t" is serialized with i as 15.

I included class A to add static "a" in test class to check that static varaibles are not serialized, but still static "t" is getting serialized.
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121



Output:
15
5

You see? static "a" is not serialized, so even after changing its "x" from 5 to 7, upon deserialization "tt.a.z" returns 5, it's default value.
So how can static "t" get serialized?
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You are probably confusing yourself because you have two test objects one inside the other. Create a separate class with just the main method to clear things somewhat.
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

E Armitage wrote:You are probably confusing yourself because you have two test objects one inside the other. Create a separate class with just the main method to clear things somewhat.


Can you please explain what are you saying? Because from what I understand, static "t" is getting serialized. If not, what code would you write to justify it?
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

I tried some codes which shows that if you save a static variable as a part of the object, it doesn't gets serialized, but if you pass static variable directly to the method writeObject(), it get's stored to the file. Because you are not writing the object itself to the file, instead you are writing the contents of that object to the file.



prints output - 11
static test object declared at line1 didn't get serialized with the test object.


Astha - OCPJP 6 (90%)
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Astha Sharma wrote:I tried some codes which shows that if you save a static variable as a part of the object, it doesn't gets serialized, but if you pass static variable directly to the method writeObject(), it get's stored to the file. Because you are not writing the object itself to the file, instead you are writing the contents of that object to the file.



prints output - 11
static test object declared at line1 didn't get serialized with the test object.


But in AnotherClass's serializeTest() method you declared t again:

which basically is a "new" test instance that'll have 11 as the default value of i. You are serializing a different "t" instance and not the "t" from test class that has i = 15;
That is why your output is 11.

Any more suggestions or any idea how is the static instance serialized here?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in Serialization