• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why this difference is Deserialization?  RSS feed

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please consider the following code where i am serializing an object which has few static variable and static references.



When i execute this code i get the output as


I am in Employee private constructor
Before Serialization
int_variable=2
transient_variable=3
static_variable=4
This is salary object
After Serialization
int_variable=2
transient_variable=0
static_variable=4
This is salary object


But when is change the code of class test to only deserializing i get null pointer exception. Here is the code


and the output is

After Serialization
int_variable=2
transient_variable=0
static_variable=3
Exception in thread "main" java.lang.NullPointerException
at Employee.printValues(AbstractClassTest.java:52)
at test.main(AbstractClassTest.java:71)



Why is this difference? we know that static variables or references are not serialiazed as they are part of class. But during deserializing when i am able to print the value of static_variable, why is the code failing to execute the printSalary() method? Please clarify?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you deserialize, the serialized class' constructor will not run again. As your Salary reference is static, it will not be serialized. You assign a value to sal in your constructor and thus sal=null when you deserialize when the constructor has not run yet. Create a new instance of employee or initialize all the static fields before you try to access members of the deserialized object.
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petrus,
Static variables or references are part of class hence they are not stored during serialiazation process. So when i serialize the Employee object , salary object will not be serialized as it is referenced by static variable. But sal reference is part of Class Employee and hence in my 1st output, when i deserialize the Employee object i am able to print Salary class print method because the salary object is still avialable in the Heap and is referenced by Employee class sal static reference.

But in my 2nd case, where i am only deserialzing why am i getting Null pointer exception even though its Employee class that holds static Salary sal reference which must be still available in the heap. Why is this so? Is it that Employee class got unloaded immediately after the serialiazation process was done. And when i deserialized 2nd time, class Employee was loaded and sal was set to default values which happens to be NULL? Please clarify.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Employee class will stay loaded until there are no more references to the class. I assume that the second part was run after the first one was finished. This means that the Employee class was no longer available and had to be reloaded. As you did not assign the sal reference the second time, it will be equal to null. Thus you will get a NullPointer exception when trying to call members on sal.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satish Kota:
And when i deserialized 2nd time, class Employee was loaded and sal was set to default values which happens to be NULL? Please clarify.


Exactly! Except the default value does not "happen to be null" - it is null.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!