• 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

Problem in Serialization

 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output is:
15



Static variables are Class variables and hence not serializable. But in the above program static instance t gets serialized. How?
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization topic were removed from OCPJP 6
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic