• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.io.Serializable;  RSS feed

 
Nicola Guy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anybody explain what this does and why its needed to me??
 
Dan Pollitt
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializing an object means writing it to disk or a byte stream so that it can be persisted or sent to another JVM. At a later time the object can be read in an used.

Implementing the java.io.Serializable interface but adding no additional methods means you are stating that the object can be serialized by Java's default mechanism.

you should have a look at http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
Nicola Guy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it necessary for validation?
 
Dan Pollitt
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "Validation"?
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Pollitt:

Implementing the java.io.Serializable interface but adding no additional methods means you are stating that the object can be serialized by Java's default mechanism.



Precisely. First, you should be aware that one of the ways Interfaces could be defined is "Conformance to requirements". This means, the Java language says that "if your class conforms to some particular requirements, then it will be allowed to, or be able to carry out specific operations". This is the most general way of describing an interface.

As Dan Pollitt has rightly pointed out, by implementing the Serializable interface, you are declaring that your class' objects are going to be able to be saved to a file. The Serializable interface is a tagging interface. So, if you wish to persist your objects then you will have to implement the Serializable interface.
 
Weij Yin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it necessary for validation?


java.io.Serializable is an interface that is entirely empty. A class can implement this interface simply by naming it in its implement clause without having to implement any methods. This technology is a useful way to provide additional imformation about an object, and it does not affect the normal operation on an object.

java.lang.Cloneable is another such interface. It defines no method, but identifies the class as one that allows its internal state to be cloned by the clone()method of the Object class. For example:


Object o;
Object copy;
if(o instanceof Cloneable) copy = o.clone();
else copy = null;


Now, let's come back to java.io.Serializable interface. A class should implement this interface simply to indicate that it allows itself to be serialized and deserialized with ObjectOutputStream.writeObject() and ObjectInputStream.readObject(). In other words, the class should be converted into a stream of bytes that can later be deserialized back into a copy of the original object. When to use it? in the area of network programming and distributed programming, because they have issues such as delivery delay, difference of systems, and network failing.

Regards,
Weij
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!