[Bluej-discuss] Excercise 4.25 review please

Michael Kölling M.Kolling at kent.ac.uk
Mon Jun 19 10:49:08 BST 2006


On 19 Jun 2006, at 00:00, JD wrote:
>
> Thanks for the reply.  In the end I I knew I was making an external  
> method call...It just that I didnt know you could call a local  
> object reference highestBid that calls another object reference  
> getBidder() in an external class Bid that calls another object  
> reference bidder  in another external class Person that FINALLY  
> calls a real method getName that returns the bidders person  
> name....whew!.... I hope this book explores more examples of these  
> chained external method calls as I am sure they are common to use.

Maybe the easiest way to understand the line of code you are talking  
about is to pick it apart. You had:

	highestBid.getBidder().getName();

The result of that is a string, and you could store it in a local  
variable, if you want:

	String name = highestBid.getBidder().getName();

You could write the same in two lines as:

	Person bidder = highestBid.getBidder();
	String name = bidder.getName();

Here, I take my bid 'highestBid' and call the 'getBidder()' method.  
This gives me a person, and I store that person in the 'bidder'  
variable. (That's the first line.)

Then I take the bidder (from that variable) and call the 'getName()'  
method. This gives me a String, which I store in another local  
variable (called 'name').

Now if you look at the previous version, it goes like this:

	String name = highestBid.getBidder().getName();

Again, I start with my bid in 'highestBid', and I call the 'getBidder 
()'method. Again, this will give me a Person object. But now, without  
storing the Person object anywhere (while I still hold it in my hand,  
if you will) I just go ahead and straight away call the 'getName()'  
method on it. This gives me a String, which I then assign to a local  
variable. I just drop the Person object on the floor, because I don't  
need it anymore.

You could chain this as much as you like: you could have move  
appended another method call for the String object you got...

Regards,

Michael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060619/53906c44/attachment-0001.html


More information about the bluej-discuss mailing list