• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting transient variable values while deserializing

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dinakar Kas
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One hint: return getInstance() from readResolve(), not emp. What if emp is still null?
 
Dinakar Kas
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Done it. Still Nope.
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic