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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User /mgs

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

Post 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.

admin
Site Admin
Posts: 8960
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest