Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.140 :

Posted: Wed Jan 12, 2011 7:56 am
by ETS User /mgs
Question is:
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
    }
}
What can be done so that a Portfolio object can be serialized while preserving the state of the Bond objects contained in 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.

Re: About Question com.enthuware.ets.scjp.v6.2.140 :

Posted: Wed Jan 12, 2011 12:43 pm
by admin
You are right. The complete code that is given in the explanation says so as well. The option should be made clearer asap.