Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11897

Re: Question about Effective Java

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: Question about Effective Java
Date 2012-02-09 16:42 -0800
Organization A noiseless patient Spider
Message-ID <jh1p6k$f4s$1@dont-email.me> (permalink)
References <Xns9FF4833D32E5Ajpnasty@94.75.214.39> <jh13se$dve$1@dont-email.me> <Xns9FF4C2ADB8478jpnasty@94.75.214.39>

Show all headers | View raw


On 2/9/2012 4:07 PM, Novice wrote:
> 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.


Patricia had an insight that I missed, which was that only a reference, 
a pointer to the object, gets returned, and indeed that's all Java uses 
is pointers, not "complete objects".

So there's only one Builder, which sits on the heap, and the return 
statement keeps passing a reference to that object back to the code.  I 
usually don't make a distinction because the practical difference is 
small.  But it's there, and can sometimes be helpful when you analyze code.

Also, "this" doesn't mean of the class Builder.  It could be a subclass; 
but inheritance means that you can't tell, and usually shouldn't care.


> 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.


Yup, unused and can't be used, because they're private.  If you also 
made them public, then some other class might use them, even if none 
actually do.  That would also make the warning go away.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about Effective Java Novice <novice@example..com> - 2012-02-09 17:54 +0000
  Re: Question about Effective Java markspace <-@.> - 2012-02-09 10:39 -0800
    Re: Question about Effective Java Novice <novice@example..com> - 2012-02-10 00:07 +0000
      Re: Question about Effective Java markspace <-@.> - 2012-02-09 16:42 -0800
  Re: Question about Effective Java Patricia Shanahan <pats@acm.org> - 2012-02-09 14:19 -0800
    Re: Question about Effective Java Novice <novice@example..com> - 2012-02-10 00:40 +0000
      Re: Question about Effective Java Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-09 21:08 -0500
        Re: Question about Effective Java Lew <lewbloch@gmail.com> - 2012-02-10 09:01 -0800
  Re: Question about Effective Java "John B. Matthews" <nospam@nospam.invalid> - 2012-02-10 11:59 -0500

csiph-web