• Post Reply Bookmark Topic Watch Topic
  • New Topic

transient  RSS feed

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers..

i am going throgh K&B certification book... i am at collections and objects.. there i got stuck in hashcode along with transient.. wat are these transient.. how are they used.. give me an example the way it differs from other modifiers.. and the relation with hashcode and transient variables.. one thing which i am able to get from the book is that transient variables are not to be used inside the overriden hashcode().. pls help me out... with examples..
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
transient data members of a class aren't serialised when serialising a in instance of a class that implements Serializable.
That's all there is too it.

You'd usually not include them in hashCode calculations either, especially if you want to make certain that the hashCode of a specific instance doesn't differ depending on conditions outside the sphere of influence of the class instance.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you could give me some example of transient variables i will be thankful to you..
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably a bad example for the real world, but bear with me.

Let us imagine that I have a data object that I want to be able to store as a serialized object (in an HttpSession or some such). This object will hold String values of firstName and lastName and I want these to be stored when it's serialized because they will be accessed often and it should be quick. But I will also have a picture of the person that I DON'T want stored because it'll be too big and won't be accessed that often. So I can create that class like so:



If the firstName and lastName values are called after it's deserialized, it will already have the values and not need to do anything special. But if someone wants the picture, I can load it in and return it. If the picture is requested again before it's reserialized, it's already in memory and I can ust return it. If it's serialized though, picture will be set to null and not saved because it's set as transient. That will save space in memory.

Hopefully that's a good explanation.
[ May 05, 2006: Message edited by: Joseph Erickson ]
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u joseph.. i got it.. so transient is mainly for saving memory right???
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, it's intended mainly to prevent potentially dangerous data from being sent over network connections or retained for the future.

Say you're building a class instances of which have to be sent over a network to other computers.
Now that class has a variable in it that's a reference to a network service.
You make the variable transient so it's not sent to your clients.

That object could for example be an EJB sessionbean. The object holds a reference for calling when needed, but the handle to the bean has no meaning to your clients so they'll have to supply their own.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if your object has a reference to some other object that is only meaningful on the system where it is now located -- such as a File or a JDBC Connection, for example -- then it doesn't make sense to serialize that other object. Deserializing it on a different system would at best result in garbage. So you would declare those references as transient.

So, no, it is nothing to do with saving memory.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!