The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jkorman (1) [Avatar] Offline
In your answer to this puzzle you say that the class AddressBook is mutable through the getNames method. However your usage in the answer

String [] names = book.getNames()

is not consistent with the declaration in the puzzle;

String getNames(int n)

The class AddressBook needs a method

String [] getNames()

to be published.

In addition the names attribute in AddressBook is mutable through the original array that was used to construct the AddressBook.

String [] names = {"Fred", "George", "Mary", };

AddressBook book = new AddressBook(names);
names[1] = "Cindy";

book.getName(1); // opps

Good work so far. I'm looking forward to the completed book.

dhanji.prasanna (37) [Avatar] Offline
Re: Chapter 10 - Immutability Puzzle 2

Thanks for the kind words.

Good catch on the getter name! I've fixed that in the upcoming draft.

Yes, Puzzle #4 describes exactly that scenario! There's just no way to safeguard an array (directly) from being mutated, accidentally.