Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable

 
gr Chinna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is Serializable in java?i want desription abt Serializable and how to use?and why we have to use?
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

'Serializable' is a marker interface. A marker interface is one that has no methods. By implimenting a marker interface, you're telling the JRE that your class has specific properties. For example, if your class implements Serializable, then the JRE knows that it doesn't have to do anything special to serialize it. (convert an object of your class into a byte stream) Hope this helps.

_steve.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Also "googling" will help more...

Regards,
Maulin
 
Roshini Sridharan
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

For permanent storage of object details (Object State) outside the program scope (i.e. store to a file/ pass the object across the network) we implement the class with Serializable marker interface as follows.

class Test implements Serializable{
.....
}

Generally, basic data types can be literally stored into any file permanently, when it comes to object state, Java provides an inbuilt way of storing the instance variables of the objects to any stream using Serializable Interface.

Hope this helps.

Regards
Roshini
 
Myra Rose
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still not getting the concept of using Serialization in Java.

Can anybody eloborate?

When do u need feel to serialize an object. If I have an employee object and want to store it in database, do I need to serialize employee object?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ritika Agarwal:
If I have an employee object and want to store it in database, do I need to serialize employee object?


That would be one way, but not the only way, to do it.

Just one common example: serialization is often used to save and migrate user sessions in a servlet container. Imagine you have a whole server farm with machines A, B, C, and D. Machine A handles a request, and after the request, stores the user session into a file on a shared disk using serialization. Machine C gets the next request from that user, so loads the session and services the request. It's easy, simple, and doesn't require the server to know anything about what's in the session.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See The Java Tutorial:

Object Serialization
[ July 28, 2006: Message edited by: Jesper Young ]
 
Myra Rose
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


That would be one way, but not the only way, to do it.



What can be the other possible way?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ritika Agarwal:

What can be the other possible way?


"Object-relational mapping". If you have a large number of instances of class X to store, then you can define a database table X with columns that correspond to the fields of X. To store an object, you create a new row in the table, saving each field into the apropriate column. To retrieve an object, you create an empty one, and store the data from one row in the table into its fields.

There exist many software packages that do this automatically -- Hibernate is a very well known one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic