aspose file tools*
The moose likes Java in General and the fly likes POJO implements Serializable - Why ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "POJO implements Serializable - Why ???" Watch "POJO implements Serializable - Why ???" New topic
Author

POJO implements Serializable - Why ???

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
I have seen in many of the POJO(Plain Old Java Object) that it implements Serializable Interface.

can anybody tell me what is the use of implementing Serializable interface in POJO ??

Can anybody tell me what makes java class a POJO ?


Jigar Naik


Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Jigar,

I guess you are just confused because the term POJO is often used interchangeably with JavaBeans. As the name suggests a POJO in general is a plain old Java object for example in contrast to Enterprise JavaBeans in EJB version 2.x which had to implement or inherit from cumbersome API classes and interfaces.

It's arguable if a POJO is the same as a JavaBean but at least the rules to create JavaBeans is where the Serializable interface comes from. Please take a look at the API. Another characteristic of JavaBeans you will be more familiar with are the well-known getters and setters for class members

Marco
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
Hi Marco,

Thanks of the quick reply... Actually my question is not in terms of EJB 2.0.

Assume that i am not using any frame work.

I have a simple web application which uses Servlet as controller. and jsp as view

And i have Class Employee which is POJO which has getter setter default no argument constructor and implements serializable interface.

The job of servlet is to create one instance of Employee POJO. set the request parameter values got from JSP into Employee POJO using employee's setter method. and pass the pojo to some other method for further processing.

in this case what is the use of implementing serializable interface in Employee class.

Or implementing serializable has nothing to do with POJO ??/
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

The thing with EJBs was just to explain what would NOT be a POJO

Or implementing serializable has nothing to do with POJO ??

It has to do with POJOs as soon as you use the term POJO as a synonym for JavaBean. Classes which follow the JavaBeans conventions should implement Serializable because this marks them as being transferable over network or and Serializable objects can be "serialized" to a persistent storage.

If for you a JavaBean is exactly the same as a POJO then of course a POJO should implement Serializable. If a POJO for you is not exactly the same as a JavaBean then Serializable has nothing to do with POJOs. For my understanding a POJO is not the same as a JavaBean but instead an object or class which does not implement any special interfaces and does not inherit from any special API classes.

But don't worry to much about this naming issue. You will surely see many different interpretations on the web regarding the term POJO. Originally it was coined and used by Martin Fowler to differentiate between classes which contain just your application logic and the said classes (like EJBs) which had to implement complicated interfaces or something like this.

I hope this makes it clearer!

Marco
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Jigar Naik wrote:in this case what is the use of implementing serializable interface in Employee class.

So it can be serialized. For example, for session replication.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
in this case what is the use of implementing serializable interface in Employee class.


Servlet containers are allowed to serialize a session ANY time they want to between requests.

You have NO control over when this happens. If you fail to make an object "stored" in a session serializable, there is a chance it will vanish mysteriously. Trust me, this is hard to debug.

Bill

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
As David Newton said, "So its objects can be serialized..." (stored and retrieved).
"Serializable" is called a marker interface because it has no fields or methods; it's
entirely empty. Since the serialization process can be tricky, as there are rules
and the programmer has a lot of flexibility, it often requires quite a bit of custom
code. By implementing Serializable, the programmer is declaring that this work
has been done and the needed behaviors are in place.

Jim...


BEE MBA PMP SCJP-6
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
Thanks a lot all of you for detail explanation...

My understanding is now very clear about POJO and implementing serializalbe in POJO.

Thanks again...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: POJO implements Serializable - Why ???