This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes new to java... about serialization... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new to java... about serialization..." Watch "new to java... about serialization..." New topic

new to java... about serialization...

Sanjeev Charla
Ranch Hand

Joined: Apr 02, 2009
Posts: 90

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

Joined: Jan 28, 2003
Posts: 4181

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

Joined: Oct 13, 2005
Posts: 46434
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).
I agree. Here's the link:
subject: new to java... about serialization...
jQuery in Action, 3rd edition