Serialization confusions

Java masters please see my issue herehttp://forum.java.sun.com/thread.jspa?threadID=5170311&tstart=0
[118 byte] By [rajitoora] at [2007-11-27 3:45:13]
# 1

Your question should have been posted in this forum only.

Non-serializable classes don't have their data serialized. Serializable classes do. Non-serializable base classes of serializable classes don't. That's how it works. If you want the data of a class serialized, make the class Serializable, or else write your own readObject/writeObject methods in the serializable derived class to handle the base class data.

ejpa at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 2

well i thouht i was new to java so i posted it there

If i have to write my own read/write methods for non -serialized classes. then its going to be quite painful if the they contain a chain of instance variables refencing other nonserializable classes. So whats easy i java about serializing an object? i believe it could become quite painful in large programs.

rajitoora at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 3

That's not the general experience. It's rare to have to go to all that trouble. I've written about 10 writeObject() methods in ten years. Generally you just put 'implements Serializable' and add a serialVersionUID and it's done. Most of the classes in the JDK that should be serializabe are serializable, and the ones that aren't serializable shouldn't be. The same should go for your application classes. Many things it makes no sense to serialize, e.g. streams and sockets and connections.

For an alternative, solution better suited to long-term persistence, see http://java.sun.com/docs/books/tutorial/javabeans/longpersistence/index.html

ejpa at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 4

So you mean to say that allmost all classes except the ones like streams, sockets etc would qualify for (instance of Serializable)

and any custom programs i make i should implement Serializalble by default on their base clases by thinking that who knows when a need may arise to serialize their objects and thats what java has done?

rajitoora at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 5
I don't think I said that at all actually. What you make serializable is up to you but it should certainly include classes that might form part of a protocol.
ejpa at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 6
thankz ejp i have got what you said and hope i didn't ofend you on some sharp replies.
rajitoora at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 7

Non-serializable classes don't have their data serialized. Serializable classes do. Non-serializable base classes of serializable classes don't. That's how it works. If you want the data of a class serialized, make the class Serializable, or else write your own readObject/writeObject methods in the serializable derived class to handle the base class data.

Message was edited by:

jamsheerkn

jamsheerkna at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...
# 8
Why are you repeating what I said in reply #1?
ejpa at 2007-7-12 8:48:55 > top of Java-index,Core,Core APIs...