Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Novice Newsgroups: comp.lang.java.programmer Subject: Re: Question about Effective Java Date: Fri, 10 Feb 2012 00:07:36 +0000 (UTC) Organization: Your Company Lines: 81 Message-ID: References: NNTP-Posting-Host: /kSec8Zpu/mSG50nCPQbWQ.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: Xnews/5.04.25 X-Antivirus-Status: Clean X-Notice: Filtered by postfilter v. 0.8.2 X-Antivirus: avast! (VPS 120209-3, 2012-02-09), Outbound message Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11894 markspace <-@.> wrote in news:jh13se$dve$1@dont-email.me: > 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. > I understand that chaining is what we'd like to be able to do. I'm just not clear on why "this", which from the method signature is apparently a Builder, is being returned by those methods. It _looks_ like each of them is returning an entire Builder even though each only contributed one "fact" to the 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. > > > 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 > } > Right; I understand that already. The phrasing of my question is probably weird because I can't quite understand what is happening in the example. >> >> 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. > But the body of the NutritionFacts constructor is already setting values for the variables. I proved that to myself by changing the name of one of the NutritionFacts final variables and causing a compiler error in the NutritionFacts constructor. Why would getters be needed if the constructor is already setting values for the variables? > >> 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. > So if I only initialize it and never read it after that, I can still get an "unused" message? If that is right, it explains why the NutritionFacts constructor code doesn't prevent the message. I could have sworn that I've made that warning go away in the past by simply doing an assignment statement to give the variable a value, even if I never touched it again later. Maybe I'm misremembering that.... -- Novice