This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Object & Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object & Serializable " Watch "Object & Serializable " New topic
Author

Object & Serializable

Leo Tse
Greenhorn

Joined: May 13, 2002
Posts: 6
Hi all,
I have a interface that I like to use with RMI, and one of the parameters to a method (i.e. methodX) in this interface is an Object. But since Object doesn't implement the Serializable interface, I'm getting java.io.NotSerializableException exception. And I can't really change the type of the paramter, because I'll be passing different objects to this method (methodX) such as String, as well as objects I created. Does anyone know of a solution around this problem? Any suggestions would be appreciated!
thanks.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, can't you simply let the parameter type be Serializable instead of Object?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Leo Tse
Greenhorn

Joined: May 13, 2002
Posts: 6
No because the method takes different objects as arguments, such as String, and String is derived from Object... not Serializable. And Serializable is an emtpy interface, so its not the best interface to use as argument. :roll:
Brent Worden
Ranch Hand

Joined: Nov 26, 2001
Posts: 50
No because the method takes different objects as arguments, such as String, and String is derived from Object... not Serializable.

String implements Serializable, so Ilja's suggestion will work.
And Serializable is an emtpy interface, so its not the best interface to use as argument.
If you want to enforce that an argument must be serializable, then using the Serializable interface as an argument type makes perfect sence.
[ June 06, 2002: Message edited by: Brent Worden ]

Brent Worden
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It seems to me that Serializable is the perfect choice because every object you send must be Serializable.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
I often wonder why they didn't make java.lang.Object serializable ??? that would make life alot simpler imo!
The things you don't want to be serializble (if nescessary) can still be made 'transient'

Dave
Leo Tse
Greenhorn

Joined: May 13, 2002
Posts: 6
Sorry for the causing some confusion regarding my question, and I do agreed that I could make the interface accepts "Serializable" as an argument, and everything would be fine, but I do want to limit the argument to a certain type, not just any serializable objects.
And I believe there's a page on sun's site about why they chose to leave out Serializable from Objects, so prolly find the best explanation of such there
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If object was Serializable then every object would be Serializable!
 
Don't get me started about those stupid light bulbs.
 
subject: Object & Serializable