This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object & Serializable

 
Leo Tse
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, can't you simply let the parameter type be Serializable instead of Object?
 
Leo Tse
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that Serializable is the perfect choice because every object you send must be Serializable.
 
Dave Van Even
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If object was Serializable then every object would be Serializable!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic