• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

why transient variable cannot be serialized

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

why transient variable cannot be serialized?

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


bhuvi
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you please explain it more clearly


bhuvi
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I assume that you know what serialization is?
 
bhuvi mdu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization is Saving the state of object to persistent place like file.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The image objects are not stored in the file during serialization but you can build it again through the images in the HDD.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx for ur responses

bhuvi
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
We don't have time to be charming! Quick, read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic