This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KB queston pg 5o1

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Keyboard
{
}

class Computes extends Keyboard implements Serialoizable
{
KeyBoard k = new keyboard();
public static void main(String a[])
{
Computer c = new Computer();
c.storeit();
}

void storeit()
{
try
{
ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("myfile"));
os. writeObject(c);
os.close();

System.out.println("dine");

}
catch(Exception x)
{
System.out.println("exc");

}
}

Answer - exc

Can anyone please explain the output..
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the error you get ?

From a glance it appears that the entire object graph is not serializable and that is why an exception is thrown.
 
Yaroslav Ashurin
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Dinesh,
The Keyboard class does not implement Serializable interface; Computer class has-a Keyboard, and Runtime error happens when you attempt to serialize the c object. Declare Keyboard k as transient, that should help.

Regards,
Yaroslav.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic