• Post Reply Bookmark Topic Watch Topic
  • New Topic

Magic behind the serializable interface

 
Venkat Sadasivam
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to understand the concept behind the java serialization.

I know that java serialization is required when you want to save state of an object outside JVM. My thinking was that why don�t Sun make all classes as Serializable by default? Why serialization is required to persist an object?

Read more
http://venkatsadasivam.wordpress.com/2008/01/27/what-is-the-magic-serializable-interface-does-in-java/
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkat,

the Serializable interface is just a marker interface. You can implement it to tell the JVM that your class is allowed to be serialized. Nothing more. The interface doesn't provide any methods to implement it's just to mark your class.

The built-in types of Java and many classes of the standard library are serializable. But for your own classes you as a programmer have to decide how your classes should be serialized. This way you can exactly define what and how to serialize the state of an object which obviously isn't the same for every class created by any user. So it's obvious that there's no default way to serialize an arbitrary class.

Usually an object has to be serialized if you need to get the state of an object (e.g. member variables) out of memory for example if you want to save an object or object graph to disk or send it over a network connection.

Marco
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Venkat Sadasivam:
My thinking was that why don’t Sun make all classes as Serializable by default?

Because it doesn't make sense to serialize some objects.

Input- and OutputStreams for instance - these have some sort of file handle to read from. That file handle is closed when the stream becomes unavailable. So how would you serialize that? Make the file handle transient, and reinitialize it when you're deserializing? Then why not recreate the entire stream?

Another example is Process - this is associated to an OS specific process. It doesn't make sense to serialize those; a few seconds later the process can already be gone, let alone days or weeks later.

In fact, I find that Sun has been generous enough making all the AWT / Swing components serializable.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why serialization is required to persist an object?


You just need to 'mark' a class serializable and ensure that its object graph is serializable as well. Not all classes need to be Serializable. It is also a good practice to ensure that you override the serialversionuid of the objects that you wish to serialize, in case you persists them and use them across programs/jvms.
 
Venkat Sadasivam
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can realize marker class doesn't have any implementation, it just indicates the class can be serialized. It is possible to make all classes as serializable by default. Sun introduced Serializable interface to enforce developer make informed decision on the following issues.

a) Using serialization concept an object can be exposed outside jvm which potentially expose private variable values which is violation of OOPS concept.

b) JVM serializes complete object navigation graph. Sometime developer might intent to persist only top level object and forget to mark member variables as transient.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!