Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Question about Effective Java Date: Thu, 09 Feb 2012 10:39:10 -0800 Organization: A noiseless patient Spider Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 9 Feb 2012 18:39:10 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="14318"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wymWjmnP9CpNR9Fd61Hwbip/iQq12U68=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20120129 Thunderbird/10.0 In-Reply-To: Cancel-Lock: sha1:ZclgH4QTG3+iNi2Va5MPUWhCv6c= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11883 On 2/9/2012 9:54 AM, Novice wrote: > I'm trying to understand the example on page 14 of Effective Java by Joshua > Questions: > 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? So you can chain calls, like the example at the top of page 15. > They aren't constructors and they only > deal with one data value, fat or calories or whatever, but they apparently > return a complete Builder. They return a Builder object (which might be the same thing as saying "a complete Builder"). Constructors don't have a return type explicitly. Learn Java syntax. public class Builder { public Builder() {} // constructor public Builder doIt() {} // method } > > I'm also having trouble understanding some of the compiler warnings given > to me by Eclipse (3.7.1) when I actually code the class the way he has it: > - I get a "not used" warning for all of the private final variables in Right, it's just a dummy example. If you add getters to NutritionFacts, those warnings will go away. > NutritionClass (Eclipse offers to delete the variables OR create gettsrs > and setters OR "add @SuppressWarnings - unused" for each of them. I'm not > fond of any of these options but what is the best thing to do and why? Why > does Eclipse think they are unused when the NutritionFacts constructor is > clearly assigning values to them? It's a Java thing. Unused means can't be read, not is not initialized. Things that are initialized and then never used merit a warning message.