Consider the following code:
Code: Select all
class Bond
{
String ticker; double coupon; java.util.Date maturity;
}
class Portfolio implements Serializable
{
String accountName;
Bond[] bonds;
}
public class TestClass {
public static void main(String[] args) throws Exception{
Portfolio portfolio = // get portfolio somehow.
// serialize portfolio
}
}
And correct ansswer marked by Enthuware are :
1.Just have Bond class implement Serializable.
2.Implement readObject and writeObject methods in Portfolio and read and write the state of Bond object explicitly.
First answer is ok but the second is not.
First:
I mean if it states "JUST" have Bond class implement Serializable.It means to me that You can do just that (implement Serializable) and you don't have to do anything more and it'll be ok.That's true.
Second:
Because if You won't do Bond array transient it will throw java.io.NotSerializableException: Bond. So answer 2 should be :
2.Implement readObject and writeObject methods in Portfolio and read and write the state of Bond object explicitly and make Bond array transient, or sth like that.