Forums Register Login

Regarding Serialization!!

+Pie Number of slices to send: Send
Hey Ranchers!
Have a look at the code from K&B.

Now as you see Dog implements Serializable, i've 3 questions on this!!

1. How does this serializability will be useful ?? I mean what are the practical aspects of serializability ??

2. How do i use the Dog class in another code, so that serializability is applicable??

3.What does defaultWriteObject() and defaultReadObject do? Please explain in detail!!

Expecting an earlier answer!!
+Pie Number of slices to send: Send
Hi Palla,

Originally posted by palla sridhar:
Hey Ranchers!
Now as you see Dog implements Serializable, i've 3 questions on this!!

1. How does this serializability will be useful ?? I mean what are the practical aspects of serializability ??

2. How do i use the Dog class in another code, so that serializability is applicable??

3.What does defaultWriteObject() and defaultReadObject do? Please explain in detail!!

Expecting an earlier answer!!



1. First, think outside the box from a user's perspective. Imagine you are updating a database with a lot of fields, or using a word processor updating a large document. Suddenly, the fire alarm in the building goes off. You quickly click the Save button and run for your life . After such an exciting day, you come back and want to pick up where you left off. Your machine is off because the fire department shut off the electricity in the building. No problem, after booting your machine, simply run the application and File->Open your life's work.

Because a programmer used Serialization in response to the Save event, the state you left your data in is able to be read back in and you can merrily keep going.

2. Using example 1, the data would be stored in a class (and subclasses) in memory. Use the try/catch code around the defaultWriteObject() and defaultReadObject() methods with some glue and elbow grease.

3. This will cost you a beer . Since you found the code for it in K&B, chances are there's a description in the surrounding pages .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
+Pie Number of slices to send: Send
Hey Doug!
W.r.t your 3rd answer

3. This will cost you a beer . Since you found the code for it in K&B, chances are there's a description in the surrounding pages .

, what is explained in K & B is that those 2 methods (defaultWriteObject() and defaultReadObject()) do some default serialization. But i dont understand what is DEFAULT SERIALIZATION, does it serialize the Dog object ?

W.r.t to your 2nd answer

2. Using example 1, the data would be stored in a class (and subclasses) in memory.

I'd like to know how to use this class in my code which use Serializability.??

Also i heard that once you serialize a object, you can use the state of the
object to call in another JVM !! Am i right??

If so how do i use the states between 2 JVMs. Could you give an example ??
+Pie Number of slices to send: Send
Hi Palla

1. How does this serializability will be useful ?? I mean what are the practical aspects of serializability ??

Practical aspect of serializable is when you are using some variable of a class like dog in your case and you want to save the state of all the variables of class dog that so that what ever manipulations are done on dog object you have the original version saved with you. so now what you can do to for that is save that object and how do you do that in java it is done through use of Serializable interface so that when ever later on in the day you want to use that dog object which was initially created you can do that.

2. How do i use the Dog class in another code, so that serializability is applicable??

In some other class you will create an object of type dog and initialise all the other variables of that dog object. now when ever you want to serialize that dog object


to de serialize




3.What does defaultWriteObject() and defaultReadObject do? Please explain in detail!!

these are to make your serialization customizable
like in dog definition might be like this\
class dog implements Serializable

so now there might be a case that some objects defined in class dog are not serilzable (objects of other class are defined in class dog which do not implement serilzable) in that case you would like to customize the serilization the way you want to save the state of objects of a dog class .
for that scenario these two methods are provided which comes into picture in some other class serilize or deserilize objects of dog class these methods are automatically called .

I hope it clears your doubt about serilizable
so
+Pie Number of slices to send: Send
Hello Charandeep,


You want to say we can override or provide the implementation for defaultWriteObject() and defaultReadObject() if we want to manually serialize Dog class ???

Is it wright ?

when i compile and run above example, then i see that if i commented the defaultWriteObject() and defaultReadObject() method call in readObject and writeObject then also Serialization - deserialization process is carried out successfully.

Can you give me a proper example of defaultWriteObject() and defaultReadObject() method ???


Thanks.
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1241 times.
Similar Threads
Getting an exception(Serialization)
Serializable Problem
Doubts in Serialization
Serialization in java
Serialization
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:28:02.