This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes serialization and transient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "serialization and transient" Watch "serialization and transient" New topic
Author

serialization and transient

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Hey,

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

Thank You
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
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.


SCJP 1.5(97%) My Blog
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Jun 11, 2007
Posts: 327
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

Joined: Jul 15, 2009
Posts: 537

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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Jun 11, 2007
Posts: 327
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

Joined: Jul 15, 2009
Posts: 537

This is the entry level SCJP concept. Well SCEA deals with JAVA EE.
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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

Joined: Jul 15, 2009
Posts: 537

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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Jul 15, 2009
Posts: 537

For what......Well if there is any thing you have to tell please write it over here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
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.


Experience and talent are independent of age
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Good grief, you are embarrassing me now
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Apologies accepted, but it is too complicated for a short explanation. The second link I posted yesterday may help.
Amit Nimbhorkar
Greenhorn

Joined: Sep 11, 2008
Posts: 6
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??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: serialization and transient