user1343730
user1343730

Reputation:

simple query about core java serializeable interface

Can anyone tell me What is the Use of serializable interface, I googled it and read in many books that java internally uses this, but it was not explained more than this, but when we see serializable interface it is a marker interface and there is nothing in it's body,if we don't implement this interface then we are not serializing out objects.

Upvotes: 4

Views: 234

Answers (3)

shrey
shrey

Reputation: 863

I would like to explain you using practical example, i was also same like you i googled so many times to understand serialization perfectly.

First consider a scenario that i want to develop application in which it will display no of user who are online. I am using tomcat cluster environment.

What i done is i have created map when user log in i will insert his user id in map and iterate through map to display list of users online. Now main problem is first user log in through tomcat 1 and second user log in through tomcat 2. In this case there will be two different object of map in tomcat 1 and tomcat 2 created by jvm so i will not be able to display no of user online properly.

To solve above problem 1. I will write map to serializable file and check if no data exist in map i will write to file and if there is data then i will read data from file and put in map.

Main use of serializable is convert object state to file when you are sending data on network.

Upvotes: 0

Greg Kopff
Greg Kopff

Reputation: 16605

The Serializable interface is a marker interface only. What does this mean? Well, to serialise an object, you use code like this:

final Foo interestingObject = new Foo(42);
final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

oos.writeObject(interestingObject);
oos.close();

When you pass an object to an ObjectOutputStream, it uses reflection to do several things. Firstly, it checks if the object passed in implements Serializable. If it doesn't, writeObject() throws a NotSerializableException. Then, after the ObjectOutputStream has determined the object should be serialised, it reflectively examines the contents of the object, recursively serialising each of the fields (except static fields and fields marked as transient).

So you see, the interface is a way of indicating that this object should be serialised. If you don't explicitly say the class can be serialised, then an ObjectOutputStream won't attempt to serialise it. But because an ObjectOutputStream uses reflection to examine the internals of the objects in question, the Serializable interface doesn't need to specify any methods -- ObjectOutputStream doesn't need invoke a particular method.

(That's the simplified version anyway -- there are also a bunch of methods with particular signatures that you can define, and the serialisation mechanism will call them if they exist. If you're interested, read the ObjectOutputStream Javadoc.)

Upvotes: 3

Andrey Nudko
Andrey Nudko

Reputation: 707

The answer is in your question - "it is a marker interface". This is merely a hint for serialization mechanism, that verifies that everything in the object graph is Serializable and throws exception when founds something that is not. The reason is, though basically every object can be serialized, not everything makes sense to serialize (e.g. Socket store native file descriptor - if you will serialize that, send to another machine, and deserialize there, it will be absolutely useless). So the decision what should be serialized must be made by developer, and developer manifests that decision by implementing Serializable.

Upvotes: 1

Related Questions