• 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

serialization and transient

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

Can someone please explain me what is above two really means...practical application in java? and the connection between them?

Thank You
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine you have an application that holds User objects.

Now if the program is closed, all information would be lost, so you have to find a way to persist data.

You could write each property of the User class into a file, separated by comma or something, but that's cumbersome and error prone.

Instead, you mark the User class as Serializable, this means it can be written to a file, as it is.
Once you restart your program you can load the file and instantiate the same User object again.

transient is a keyword for instance variables. It tells the compiler to skip these variables when serializing, they are hence not saved.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to add one more thing about rellation between seriaization and transition.
Suppose I have a serializable object which has reference to other object which is not serilizable. In this case if we try to serialize first object it will give error. We can avoid this by making refrence variable refering non serilizable object as transient.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually when we say implements serializable on a class means we are telling the JVM that the particular class is fit for serialization but if it has a reference variable of a different class which is not serializable then the JVM sees it and gives out the NoSerializationException as one of the class in the Object graph is not serializable. Making it transient means we tell the JVM not to consider this variable for serialization. Well when that object is deserialized the blueprint(class) is used to deserialize it so that transient variable gets the default value.

I guess you can take a game as an example. You are playing a game and suddenly you have some other work so you save the game. All its state is saved(serialization) and when you start the game someother time and click on the load(deserialize) it will start at that particular time when you saved it. When you load it you can load it in the same game and not in a different game.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have instance fields which are marked transient (static fields are not serialised anyway), you need to replace it when you de-serialise, otherwise it will be null and you will suffer a NullPointerException.
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

Thankx for the reply's..i have few doughts

shivendra tripathi wrote:We can avoid this by making refrence variable refering non serilizable object as transient.



1.
Suppose in Animal class which has a non-serializable Dog class

Dog transient dog=new Dog(); // is it what you are saying? i feel its awkward

If you have instance fields which are marked transient (static fields are not serialised anyway), you need to replace it when you de-serialise, otherwise it will be null and you will suffer a NullPointerExceptio



2. I think when deserialization happen variables get the default value right?
eg: int transient x=8 will be 0 when we deserialization right?
what Campbell said only true for object reference variables right?

3. I also get to know that if we serialized only a subclass only the subclass data(which means it fields ) will be serialized not the super classs..am i write?

4. Also why Serializable interface blank? with out functionality i mean methods define how come we accomplish the serialization process?


i found this in sun java,

We wanted to avoid as much as possible the problem of being able to serialize an object in one virtual machine and not being able to deserialize that object in some other virtual machine. Since the security manager is part of the runtime environment, using the security manager for serialization would have violated this requirement.



5.what is meant by using the security manager for serialization?


Thankx in advanced
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This exception happens in the has-a relationship. If a Dog has a Collar which is not serializable then even dog won't be serializable. If you take a Is-a relationship like Dog is-a Animal then if Animal is serializable then by inheritance all its subtypes are serializable. Also if Animal is not serializable but dog is serializable then only the variables of Animal will get default values as during deserialization(all member variables[references and primitive variables]) the constructor of the Animal will run and the dog constructor doesn't run as the it was serializable. Also when we say a class is Serializable means we tell the JVM that this class is Serializable and so please do the needful.

I guess K&B has a really good explanation on this.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana Dias wrote:Hey guys,

Thankx for the reply's..i have few doughts

Careful about the spelling: some people use automatic tools for translating which might have difficulty with "thankx."


2. I think when deserialization happen variables get the default value right?
eg: int transient x=8 will be 0 when we deserialization right?
what Campbell said only true for object reference variables right?

Yes, the default value for primitives is 0, for reference types null, which probably also counts as 0. But you want to avoid null references.

4. Also why Serializable interface blank? with out functionality i mean methods define how come we accomplish the serialization process?

The serialisation is done by the JVM. I searched and found this (but you might find a more up-to-date version yourself) and this. They might (or might not ) be useful to you.
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

some people use automatic tools for translating which might have difficulty with "thankx."



so thats why ranch is so concern with spellings right, newer thought about it

The serialisation is done by the JVM.



I think i will know those kind of stuff when i became more experinced java developer...like SCEA

Thank you Campbell for the answers.

by the way is Question 1 what im saying correct?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the entry level SCJP concept. Well SCEA deals with JAVA EE.
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:This is the entry level SCJP concept. Well SCEA deals with JAVA EE.



no i mean whats hapening inside JVM and kind of stuff
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually no body is that sure what happens in the JVM. This thing is to declare means tell the JVM what to do. This declaration is done by the programmer.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana Dias wrote:by the way is Question 1 what im saying correct?

Not sure. If Dog extends Animal, and Animal is Serializable, then Dog will be Serializable too. You can try writing a serialisation method and crashing it to prevent serialisation. I can't remember the details, but it is something like a readResolve method with throw new UnsupportedOperationException(); inside. But of course that means that a Dog object behaves as if it IS-NOT-AN Animal object . . .
As far as I know, writing transient won't change Dog from Serializable to not Serializable, but it will remove it from the serialised form.

What transient is meant for is for information which is intentionally discarded. Strings are Serializable.Now you don't want to pass somebody's password un-encrypted around a network, so you get rid of it, with the transient keyword. Have you ever tried opening a serialised object file with NotePad? I have; all the String objects are clearly legible on NotePad. So a serialised password could be read by anybody who takes the trouble to look for it.

I hope that helps; I don't think I have been very clear, so somebody else might be able to explain it better
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:Actually no body is that sure what happens in the JVM. . . .

Look at the two links I quoted earlier. Or C Horstmann G Cornell, Core Java 2 vol I-fundamentals (Sun Microsystems/Prentice-Hall) chapter 12. I have the 7th (2005) edition and the section you want is about pages 662-683.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For what......Well if there is any thing you have to tell please write it over here.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:For what......Well if there is any thing you have to tell please write it over here.

You want me to copy out 22 pages? Show Some Effort.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and that's why we have the ability to make links in posts. Looks more like abstraction

Serialization is a concept which I have not faced the requirement to use by far but would definitely like to know somw real world scenarios, where I might use it. Working on web apps/services, persistence is in the hands of many components.
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:For what......Well if there is any thing you have to tell please write it over here.



We have to really appreciate what Campbell like guys doing for java community..its really not necessary to put the book content as he said..showing there is something important is really enough..because i dont think those guys are paid for posting hear..(only the title saying a bartender) so instead of reply and telling us the books which are relavant and important he may finish listening to a newly released rock album or a movie lol

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good grief, you are embarrassing me now
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i meant was explain it in a nutshell.... Short way instead of reading the whole thing just to see what a JVM does that's all. I never meant to be rude in any respects to Campbell. He is has been helping many ranchers and also has helped me.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies accepted, but it is too complicated for a short explanation. The second link I posted yesterday may help.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana Dias wrote:Hey,

Can someone please explain me what is above two really means...practical application in java? and the connection between them?



Hi can you please explain the question in detail??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amit Nimbhorkar wrote:
Hi can you please explain the question in detail??



So... all the details in this topic, along with all the details in the links provided in this topic, wasn't good enough?

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic