Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in serializatiion

 
arulraj michaelraj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to have the state of object for a class, we go for serializtion concept. It means that we just implent serializable interface for our class.

Generally we don't implemment it in a class. Even if it is not implemented, the state of object(serialized)is serialized.


how it is possible?

How will take care of it?

How it is possible for java bean?


Can you please explan it...
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.Serializable is a marker interface. A marker interface is an interface which do not declare any methods in it.

By implementing this class, we let the JVM know that this class needs to be treated differently and can be serialized.
 
arulraj michaelraj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.


If we don't implement serializable interface, what will it happen?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we don't implement serializable interface, what will it happen?
Easy question to answer: you can't serialize the object. Most people producing code for use in the "real world" would implement the Serializable interface on spec, because you can get a NonSerializableException if you try to serialize an instance whose fields are not all serializable.

There is another mechanism for (long-term) object persistence: use of JavaBeans. I hardly know anything about them
 
arulraj michaelraj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Can we provide our own serialize concept by overriding method "writeObject(java.io.ObjectOutputStream)" in a class?

2. what about java-bean? sould we implement serializable interface for java bean to store the object of that bean?
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Can we provide our own serialize concept by overriding method "writeObject(java.io.ObjectOutputStream)" in a class?


You can provide your own serialize concept. But on a practical assignment you would rarely need to do it.

2. what about java-bean? sould we implement serializable interface for java bean to store the object of that bean?


Java bean is a java class that adheres to Java Bean specification. http://java.sun.com/products/javabeans/docs/spec.html

Yes, java bean classes should also implement Serializable interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic