wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Implementing Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing Serializable" Watch "Implementing Serializable" New topic
Author

Implementing Serializable

Yed Su
Greenhorn

Joined: Mar 02, 2011
Posts: 10

Is it mandatory to implement Serializable interface if an object is to be serialized ? I worked with ObectInput/ObjectOutput and FileInput/FileOutput without implementing Serializable interface for my class and it worked perfect. Just curious to understand the Serializable.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Hi,

Yes, to be serialized, a class must be marked Serializable. If you observed otherwise, I suspect either (1) you weren't actually using serialization, or (2) your classes inherited Serializable from a parent class.


[Jess in Action][AskingGoodQuestions]
Yed Su
Greenhorn

Joined: Mar 02, 2011
Posts: 10

Thanks Ernest,

But I dont have any super class and Im using serialization as well. Please check the following example:

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39


Hashtable instances, along with String, Integer, and Double instances are all Serializable -- try putting something that is not serializable into that hashtable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Hashtable, String, Integer, and Double all implement Serializable, so all the objects you're writing are, indeed, serializable.
Yed Su
Greenhorn

Joined: Mar 02, 2011
Posts: 10

Ernest and Henry Thanks for making me realize Serialize.. !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Implementing Serializable
 
Similar Threads
Entity implements serializable
what is the output
Re: Serializable
Serialization
should an interface implement Serializable