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.
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.
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: 123, 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).