aspose file tools*
The moose likes Java in General and the fly likes Using static variables in Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using static variables in Serialization" Watch "Using static variables in Serialization" New topic
Author

Using static variables in Serialization

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 130
I have read that static variables are never serialized. But, when I wrote a program using static variables, I was able to serialize them and read them back again. What is the reason for the same? What is the correct behavior of static variables in serialization?


Below is the code snippet:

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Try inserting this at line 34:

What happens? What does that tell you?
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 193
dear mike,

i did not understand what difference -1 will make? the original question was how and why he is able to serialize static data.

everywhere in books it is said that static data is not serialized. Isnt it true?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
It's not true. Running the code will give a clue as to what is really going on.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 193
@Vaibhav

First thing, you should not use this.empId when setting a static variable.

Instead you should use Employee.empId=empid;


This will not make any difference, but its not considered a good programming practice.
Because it confuses the developer and he can assume it as an instance variable, if he is in a hurry

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 193
I got the answer to your problem.

Write the object into .ser file using the first code snippet.
Now comment the first part of the code and again try reading it from the .ser file.




The output you will get is
0
TestEmp


This clearly indicates that static variable is not stored in the file, but the the value of the static variable that is currently loaded into memory is being picked up, when you use emp1.getEmpId()

If you are still confused about it, try this





The output is

12
TestEmp
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 193
Even mike told us the same thing and it could have been clear if you would have tried his solution.
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 130
Yes. It is clear now. Thanks mike and yogesh. Actually, i missed a smal thing to notice that during successive runs, the same value was asigned again to the static variable and the value printedcwas assigned from the program itself rather than the serialized file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using static variables in Serialization
 
Similar Threads
How to update using Hibernate
Getting transient variable values while deserializing
doubt in reading object file
passing bean object to applet as input parameter
HashMap entries with duplicate keys...