• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serailaizable code example  RSS feed

 
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir/ Madam

Please find below a piece of code:




The output according to the book is that "exc" will be printed. Please explain why?

Regards
Mansukhdeep
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to serialize an instance, remember that all of its instance variables, except those marked as transient, have to be serializable. Which means that these variables have to implement the Serializable interface. Do you see what I mean ?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To know details about Exceptions, try to print Stack Trace.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:When you want to serialize an instance, remember that all of its instance variables, except those marked as transient, have to be serializable. Which means that these variables have to implement the Serializable interface. Do you see what I mean ?


Agreed Christophe

But since the instant private Keyboard k = new KeyBoard() is present inside the Computer class which is implenting Serializable interface, doesn't that make all its instances inside it automatically Serializable? Kindly elaborate with reference to this code example.

Regards
Mansukhdeep
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because you put a KeyBoard in your Computer doesn't make the KeyBoard Serializable. It really must implement Serializable itself for that.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as Rob said, instance variables are not automatically serializable. You have to take care of this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!