• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new to java... about serialization...

 
Sanjeev Charla
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we are going to serialize an object... the size of the serialized file is increases if we add a variable in the scope of the class.. but if we add a variable with in the scope of the member function... the size of the file in which we stored the object remains same..... the number of member functions does'nt increase the size of the file..but when we deserialize the object we get the exact state... how the serialization process stores the object...??? please explain... Thank you.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization stores just the Object state. Variables stored in methods are not part of the Object state. They exist only in the scope of the method - that is when the method begins to run the variable gets created, and when the method comes to an end, the variable (effectively) gets thrown away. Since it is not part of the object - just part of the incidental execution of the method - it does not need to be serialized. When the Object is stored and later retrieved it will just be re-created when appropriate.

Methods aren't serialized either. They are part of the .class file, and get loaded via the class loader. All that Serialization does is make sure that the necessary state of the Object is maintained and of the proper version - the JVM makes sure the Class (and its methods) are loaded and so can be executed with the data.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Local variables are not members of the class; they are created and deleted whenever the method is invoked. So they would not change the "size" of the class.
I looked for a few articles: see whether these are of any use: 1 2 3, but I think you will find better descriptions in books like Horstmann and Cornell (in my copy 7th edition vol I, chapter 12, page 666).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic