Results 1 to 2 of 2
Thread: Help understand a method
- 11-09-2011, 05:23 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
Help understand a method
It's one part of this method that i don't understand,Java Code:public void close() { for(Lot lot : lots) { System.out.println(lot.getNumber() + ": " + lot.getDescription()); Bid highestBid = lot.getHighestBid(); if(highestBid != null) { System.out.println(" Highest bidder: " + highestBid.getBidder().getName()); System.out.println(" Bid: " + highestBid.getValue()); } else { System.out.println(" Not sold"); } } }
and that is:
Why do you set a local variable of the type Bid as the method lot.getHighestBid(), why don't you just use lot.getHighestBid() directly? I've tried it and i doesn't work so something wrong is it, but I can't just understand why.Java Code:Bid highestBid = lot.getHighestBid();
- 11-09-2011, 05:37 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Help understand a method
You can do as you described, but then you have to get that highest bidder over and over again (extra method calls). In this code snippet the author decided to get the highest bidder once by using that method and store it in a local variable. It's a matter of taste and it's completely assuming that the method itself is 'idem potent', i.e. it returns the same value over and over again.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Trying to understand
By ladykrimson in forum New To JavaReplies: 20Last Post: 10-12-2010, 11:10 PM -
Help me to understand substring() method
By Ajitha in forum New To JavaReplies: 6Last Post: 06-22-2010, 09:03 AM -
Help me understand this method
By VinceGuad in forum New To JavaReplies: 4Last Post: 04-11-2009, 01:00 AM -
I don´t understand
By Manikyr in forum New To JavaReplies: 6Last Post: 02-22-2009, 11:22 PM -
Understand my logic errors and better understanding method and class creation
By freethinker89 in forum New To JavaReplies: 3Last Post: 10-06-2008, 11:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks