wood burning stoves 2.0*
The moose likes Java in General and the fly likes Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Serializable" Watch "Serializable" New topic
Author

Serializable

gr Chinna
Greenhorn

Joined: Jul 17, 2005
Posts: 2
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

Joined: Jul 17, 2005
Posts: 35
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

Joined: Nov 04, 2001
Posts: 1871
Hi

Also "googling" will help more...

Regards,
Maulin
Roshini Sridharan
Ranch Hand

Joined: Jan 16, 2001
Posts: 143
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

Joined: Jul 27, 2006
Posts: 44
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

See The Java Tutorial:

Object Serialization
[ July 28, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Myra Rose
Ranch Hand

Joined: Jul 27, 2006
Posts: 44
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Serializable
 
Similar Threads
Session Variables lost!
Failed to marshal exception problem
Marker Interface???
Session at client side
Re: Serializable Class