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


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

Re: Automatic linking of related objects in constructor

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: Automatic linking of related objects in constructor
Date Wed, 29 Jun 2011 07:28:04 -0400
Organization albasani.net
Lines 33
Message-ID <iuf27u$3ne$1@news.albasani.net> (permalink)
References <eYKdnUijTfibapfTnZ2dnUVZ_uadnZ2d@westnet.com.au>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net VledSYuU06IbeewS8aiziZtWV/Cw0Td5FWViADM4RfU10BbHy3TstVH2Zq+h98WN/5LIPid1tYyvwynWQ3/78bonJw6n4dv5mj4ZYI8t74U7O0A2DQoHf8guI6UFdrEK
NNTP-Posting-Date Wed, 29 Jun 2011 11:27:59 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="r9GCcti/f0QrDJXbS3HR4IKbd8Qj4jsEirPMbxi2pBV2ndu6BZzUVCMucCmOtIznXe0y0n+NBqAm2lfPhDXPucGJStdGPm6XQ+KMfoAQRqdv4FMTwrYMCxWERYuQgd4I"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10
In-Reply-To <eYKdnUijTfibapfTnZ2dnUVZ_uadnZ2d@westnet.com.au>
Cancel-Lock sha1:7p57wUcx8INxiwnnyUMPJU8u0GE=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5772

Show key headers only | View raw


Qu0ll wrote:
> Suppose you have class A which contains a list of objects of class B and that
> the constructor for B takes in a reference to the A object which is to include
> it in said list. Is it safe to make a call to A's method addB(B b) in that B
> constructor passing in "this"? I have heard that it's bad practice to "leak" a
> reference to an object from within its own constructor because it may be in an
> invalid state.
>
> If not, how else can I automatically add the B object to the list in A without
> forcing the client programmer to explicitly call addB() given that they have
> already passed in the B as an argument?

I would have 'A' call its own 'addB( theB )' just after calling 'new B()':

   public class A
   {
     private List<B> daBs = ...;

     public void theMethod()
     {
       daBs.add( new B(this) );
       ...
     }
   }

No leakage.  The add occurs after construction of the 'B' finishes.  You are 
correct to worry about leakage.  The idiom shown here guarantees that 'B' has 
its everything done before the list addition can occur.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

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


Thread

Automatic linking of related objects in constructor "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-06-29 19:56 +1000
  Re: Automatic linking of related objects in constructor "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-06-29 19:58 +1000
  Re: Automatic linking of related objects in constructor Lew <noone@lewscanon.com> - 2011-06-29 07:28 -0400
  Re: Automatic linking of related objects in constructor Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-29 08:29 -0400
    Re: Automatic linking of related objects in constructor Tom Anderson <twic@urchin.earth.li> - 2011-06-30 22:51 +0100
      Re: Automatic linking of related objects in constructor supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-06-30 18:23 -0400
  Re: Automatic linking of related objects in constructor markspace <-@.> - 2011-06-29 09:17 -0700
  Re: Automatic linking of related objects in constructor supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-06-29 19:58 -0400

csiph-web