Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 09 Feb 2012 16:19:37 -0600 Date: Thu, 09 Feb 2012 14:19:34 -0800 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Question about Effective Java References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 28 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.206.198 X-Trace: sv3-6APhYP+5RNik8cAsS/q7jJ6Sa4O/NKreczp0CGZ8hcoaeuO4drQ5oQSz0FgPaY+xz/FQJrVtLld/DXq!6ZeL9RPi2QnWv+0O4lBVcIyCQCZFbrDtdKZgue8ssUUH5hJhb2nS+X9gIWtKcPRNP3KYOYzaqQ74!L10khK/ThVW5NtFRwuVznsxAKXvokYD0dPdSWr+pCN6q6SY= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2401 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11885 On 2/9/2012 9:54 AM, Novice wrote: > I'm trying to understand the example on page 14 of Effective Java by Joshua > Bloch (Second Edition). I hope someone here can help. ... > public Builder sodium(int val) { > this.sodium = val; > return this; > } ...: > 1. I understand that a NutritionFacts could conceivably be just a > servingSize and a number of servings. All of the other values are optional > on an individual basis: any one or more of them could be there but don't > need to be. Any value that is omitted takes a default (of zero in this > example). But why are the methods calories(), fat(), carbohydrate() and > sodium() each returning Builder? They aren't constructors and they only > deal with one data value, fat or calories or whatever, but they apparently > return a complete Builder. > Don't think in terms of "return a complete Builder". Java programs do not, and cannot, pass objects around as method arguments or results. sodium really returns a Builder reference. A reference can either be null or be a pointer to an object of appropriate class. Inside sodium, "this" is a pointer to the current object. Returning it allows chaining of several method calls. Patricia