Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes why transient variable cannot be serialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why transient variable cannot be serialized" Watch "why transient variable cannot be serialized" New topic
Author

why transient variable cannot be serialized

bhuvi mdu
Greenhorn

Joined: May 03, 2005
Posts: 17
Hi,

why transient variable cannot be serialized?

Transient varaibles are temporary values so its not serialized , if am wrong please correct me.


bhuvi
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
That is the point of declaring them transient. It would normally be used when you have large data structures that won't be needed after serialization.
bhuvi mdu
Greenhorn

Joined: May 03, 2005
Posts: 17
could you please explain it more clearly


bhuvi
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Can I assume that you know what serialization is?
bhuvi mdu
Greenhorn

Joined: May 03, 2005
Posts: 17
Serialization is Saving the state of object to persistent place like file.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Ok. As an example let us suppose you have a class MyClass that has two Images that were read in from .jpg files. Now, when you serialize your instance of MyClass, you do not want to write the Image objects into it, but rather you would build the Images again when you deserialize your class instance. Does this make sense?
bhuvi mdu
Greenhorn

Joined: May 03, 2005
Posts: 17
so you mean to say that the two Image Objects in the MyClass are transient and When the MyClass is serialized, their image objects are not stored in the file and when deserialized again that two images gets formed. If its the case from where the Image Objects gets read and formed again bcos transient variable are not get stored in the persistent place, if am wrong pls. correct me.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
The image objects are not stored in the file during serialization but you can build it again through the images in the HDD.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
The MyClass/Image example was just that ... an example. I don't know how suitable it is. When you deserialize your object, the transient members will be default values - for primitives usually 0; for objects, null. You would need to provide functionality for building the values of transient members on deserialization - for instance by overwriting the readObject method of the Serializable interface.

Sun has a tutorial containing Serialization at

http://java.sun.com/docs/books/tutorial/essential/io/index.html
bhuvi mdu
Greenhorn

Joined: May 03, 2005
Posts: 17
thanx for ur responses

bhuvi
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by bhuvi mdu:
why transient variable cannot be serialized?
It's been explained a couple times in this thread, but just in case it wasn't crystal clear: Java's transient keyword tells the serialization system to ignore the fields when writing and to initialize them the same way instance fields are initialized during reading.

Here's another example from the JDK itself. The standard Collection classes introduced in JDK 1.2 can cause intermittent exceptions or simply "unexplainable" behavior under specific multi-threaded conditions, even if you synchronize all calls to its methods. One such condition is when any thread calls a method that structurally modifies it while one thread is using its Iterator.

For example, changing a value in a Collection doesn't alter the its structure. But adding a new value can. In a TreeSet, adding a new value requires inserting a new node somewhere in the tree. Any live Iterators will have an internal state that's no longer consistent with the TreeSet. Trying to return the next element might return the wrong element (skipping some or jumping back in the sequence) or one that was removed.

Sun's solution was to add an instance variable (called modCount) that tracks the number of times the Collection has been structurally modified. When an Iterator is created, it stores a copy of modCount. At the start of each method -- hasNext(), next(), and remove() -- it checks its copy against the Collection's current value. If they differ, it throws a ConcurrentModificationException.

[ Note to self: examples aren't very good if they require three paragraphs to provide background that's necessary but in the end doesn't really relate to the topic at hand. My bad. ]

In any case, if you serialize the Collection and then deserialize it some time in the future, you receive a new reference to a new Collection. It's physically impossible for any object in memory to hold a reference to it. Therefore, there's no point in storing modCount; it can safely be initialized to 0.

What about any Iterators on that Collection that were serialized with it? It would be a problem only if the Collection's Iterators implemented Serializable. I checked ArrayList's, and it isn't in JDK 1.5.0. I suspect none of them are as serializing an Iterator seems marginally useful at best.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why transient variable cannot be serialized
 
Similar Threads
More more code concepts!
static transient member variable
transient variables
final and transient
Why are transient static variables allowed?