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


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

Re: Reuse of argument objects

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Reuse of argument objects
Date 2014-04-24 09:50 -0400
Organization A noiseless patient Spider
Message-ID <ljb4r9$hvg$1@dont-email.me> (permalink)
References <objects-20140424142227@ram.dialup.fu-berlin.de>

Show all headers | View raw


On 4/24/2014 8:30 AM, Stefan Ram wrote:
>    For example, in Android, there is this interface specification:
>
> public void setLayoutParams (ViewGroup.LayoutParams params)
>    Set the layout parameters associated with this view.  ...
>      params   The layout parameters for this view,  ...
>
>    . An example usage:
>
> final android.widget.LinearLayout.LayoutParams parameters
> = new android.widget.LinearLayout.LayoutParams
> ( android.view.ViewGroup.LayoutParams.MATCH_PARENT,
>    android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1f );
>
> Main.this.increment.setLayoutParams( parameters );
>
>    I think there is something missing in the documentation:
>
>    Can the »parameters« object be reused to supply the same
>    parameter values to other calls of »setLayoutParams« with
>    other target objects? Or even with other parameter values?
>
>    Has setLayoutParams made its own copy of the parameter values,
>    or has it just stored a reference to the parameters object?
>
>    This is like a kind of ownership: Who owns »parameters«
>    after the call to »setLayoutParams«? The caller or the callee?

     If LayoutParams is immutable the question needn't be answered,
otherwise it ought to be.  If it's mutable and the documentation
doesn't provide an answer, I think you've got two choices:

     1) Look at the code for the "consumers" of LayoutParams and
        see what they do with it.  Risky, of course, because the
        next release might change things.

     2) Use a fresh LayoutParams instance for every call (a copy
        constructor or copying factory or clone() would be helpful).
        Possibly wasteful, but safe. (Make your copy *before* giving
        the original to a consumer.)

>    Or is there a common standard in the Java world for this?

     I resolutely and nobly resist the impulse to remark snarkily
upon your respect for "common standards" in coding style. ;-)

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

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


Thread

Re: Reuse of argument objects Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-04-24 09:50 -0400

csiph-web