wood burning stoves 2.0*
The moose likes Java in General and the fly likes Getting transient variable values while deserializing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting transient variable values while deserializing" Watch "Getting transient variable values while deserializing" New topic
Author

Getting transient variable values while deserializing

Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
Hi all,

I have the following code:





It works fine.

Now, I made Employee as Singleton, and serializing and deserializing it.





When I made Employee as Singleton, while de-serialization, I was able to print the department name which is a transient variable(dept in Employee).

Am I going wrong anywhere?

Thanks
Dinakar
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Transient variables are not initialized after unmarshalling.

EDIT: I mean if you use a singleton. Transient variables would be implicitly initialized to their default values if a new instance was created.


[My Blog]
All roads lead to JavaRanch
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34

Transient variables would be implicitly initialized to their default values if a new instance was created.


But, I am not creating any new Employee object here. I am only unmarshalling it.

Also, even if I create a new Object(Employee here), as I did not specify any default value in it, it should still return null.

so where can be the problem?

Thanks,
Dinakar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

One hint: return getInstance() from readResolve(), not emp. What if emp is still null?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
return getInstance() from readResolve(), not emp. What if emp is still null?


Done it. Still Nope.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I didn't say it would solve this issue. It's just very important because it will solve future errors.

readResolve means that the de-serialization process will not return the serialized object but the object returned by the method. This means that the non-serialized singleton instance will be returned, which still has its department set.
 
 
subject: Getting transient variable values while deserializing
 
Similar Threads
How to update using Hibernate
Hibernate association mapping
passing bean object to applet as input parameter
Return multiple fields aas 1 object
HashMap entries with duplicate keys...