• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialize - Exception thrown

 
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code:


Computer has keyboard, but Keyboard is not serializable --> exc.

Could the exc. be circumvented/prevented with implementation of writeObject in Computer? Like:



In the kb book there is an example with Dog and Collar classes, Dog is serializable, Collar not. Dog has an attribute of Type Collar.

The important issue is that no except. occurs if write- and readobject(s)() are implemented with each two methods default...object and ...int and in readObject() a class without constructor of the non-serializable class (here collar) is created. right?
But I do not understand if and how I can transmit this to the computer example. In the kb, the method getCollarSize is used as parameter in the writeInt() call. I do not know which method to take for the computer writeint method in writeObject(). The writeInt() needs an attribute, but Keyboard has none.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

timo corn wrote:
The important issue is that no except. occurs if write- and readobject(s)() are implemented with each two methods default...object and ...int and in readObject() a class without constructor of the non-serializable class (here collar) is created. right?
But I do not understand if and how I can transmit this to the computer example. In the kb, the method getCollarSize is used as parameter in the writeInt() call. I do not know which method to take for the computer writeint method in writeObject(). The writeInt() needs an attribute, but Keyboard has none.



Since Collar (and keyboard) is not serializable, you can't send it over the stream. What needs to be done is to send the data needed to create a new Collar on the other side -- meaning the Collar size needs to be sent over and a new Collar is created based on the size.

In the case of the keyboard, it has a no-args constructor -- and that was the constructor that was used. So, writeObject() doesn't need to send anything. And readObject() should just create a keyboard object using a no-arg constructor.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

On the other hand, if you want the remote machine to access the local keyboard, then that can't be done just by serialization. You will need to create a new keyboard type -- used to access remote keyboards. And a new service type used to service remote keyboard request.

The writeObject() needs to confirm that the service is up, and send the networking details over the stream. And the read side needs to use this new remote keyboard stub to access the keyboard across the network.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic