Results 1 to 2 of 2
  1. #1
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Help understand a method

    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"); 
                } 
            } 
        }
    It's one part of this method that i don't understand,

    and that is:

    Java Code:
    Bid highestBid = lot.getHighestBid();
    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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-13-2010, 12:10 AM
  2. Help me to understand substring() method
    By Ajitha in forum New To Java
    Replies: 6
    Last Post: 06-22-2010, 10:03 AM
  3. Help me understand this method
    By VinceGuad in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 02:00 AM
  4. I donīt understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-23-2009, 12:22 AM
  5. Replies: 3
    Last Post: 10-07-2008, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •