aspose file tools*
The moose likes I/O and Streams and the fly likes what does java.io.serializable do ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "what does java.io.serializable do ???" Watch "what does java.io.serializable do ???" New topic
Author

what does java.io.serializable do ???

karim qazi
Greenhorn

Joined: Mar 22, 2001
Posts: 20
i can implement java.io.serializable and i cannot see any diffrence when my class is executed.
i know you are supposed to implement this interface when you want to retain information a object might have but to retain the information you must write the data to an output stream and this can be done without ever implementing this serializable interface.
can anybody show an example of diffent outcomes of when a class executes when implementing the serializable interface and when not implementing it???
any help is greatly appreciated.

------------------
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Greetings, Karim
Implementing the Serializable does absolutely nothing except flag your class as being serializable. One would do this when wanting to write the object to file, or shoot over the network, COM port etc.
This confused me:

but to retain the information you must write the data to an output stream and this can be done without ever implementing this serializable interface.

You can write to an OutputStream without the Object implementing Serializable but it's not the data in the Object (at least it doesn't look like it)
For example:

If you look at both output files, I think you'll see what I mean. I do think it's strange that the non-Serializable output actually writes to file before throwing an Exception.
karim qazi
Greenhorn

Joined: Mar 22, 2001
Posts: 20
so if you implememnt serializable you can write the object to a file and then read it back in to see what values it was holding?
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
This is correct. Check out the tutorial at http://java.sun.com/docs/books/tutorial/essential/io/serialization.html
karim qazi
Greenhorn

Joined: Mar 22, 2001
Posts: 20
this was very helpful to me.
thank you very much.
so when writing a javabean for instance that you want other objects to be able to seralize meaning write to an output stream you must implement serializable?
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Absolutely, in fact, I believe that implementing Serializable is a requirement to be a Java Bean, but I'd have to look at the spec to be sure.
karim qazi
Greenhorn

Joined: Mar 22, 2001
Posts: 20
So if i were to serialize a javabean lets say a User javabean that holds the users name,password, etc.
Would it be better to seralize the beans data or hold the beans information inside of a session when using jsp?
Also when you seralize multiple beans into the same file how does the file input stream know which object you want to read in?
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
I'm not familiar enough to recommend which one is better. I have used a similar thing in my .jsp - a user bean with session scope. We access this frequently, so why not keep it in memory?
Sorry, but I've never serialized different objects in the same file, just put them to the DB. I'd have to try it and see. Maybe someone else knows??
karim qazi
Greenhorn

Joined: Mar 22, 2001
Posts: 20
so seralize the object into a database record?
if i am doing this why wouldnt i just store the data the bean holds into the database instead of storing the object?
i guess my real question is when is most applicapble to use seralizable?
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Sorry, I didn't mean serialize the bean to a database record, just put the data the bean holds to the DB.
I'm no guru, but I would say it is most applicable to use serialiable only when you have to - transferring the object across the network for example. In my (limited) experience, I've seen no reason to serialize an object to the disk.
 
 
subject: what does java.io.serializable do ???