• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

transientand Collections

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please what happens is a transient variable reference is added to a collection, can it be added and be gotton back?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot add variables to a collection. Only objects.
Bu.
 
Juwonlo Ibigbami
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what of a transient variable reference to an Object
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If you had a transient reference to your collection and then serialize this collection, the object won't be serialized so you won't be able to retrieve this object after deserialization !
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'transient' is a property of the variable; not of the object that the variable refers to. As Burkhard says, you don't add a variable to a collection. You can add the value of a variable to a collection. The object that the variable refers to, doesn't know that the variable is transient.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All collection classes implement Serializable, so you can e.g. serialize an ArrayList.
If you want the contents stored inside back, also this objects need to be serializable.

The main reason why a variable is marked transient is that the object it holds is not serializable.
You won't be able to serialize the collection if an object that's inside is not serializable. You would get a otSerializableException.

If it is transient, you will get your collection back, and where the transient variables were you'll get null:


Example:


output is:
before:
[Cat with 9 lives, Cat with 7 lives, StrangeCat with 9 lives and with a Mouse: Mouse@130c19b]
after deserialization:
[Cat with 9 lives, Cat with 7 lives, StrangeCat with 9 lives and with a Mouse: null]



StrangeCat also implements Serializable (by inheritance).



Yours,
Bu.
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic