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


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

Re: Reuse of argument objects

Started byEric Sosman <esosman@comcast-dot-net.invalid>
First post2014-04-24 09:50 -0400
Last post2014-04-24 09:50 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#28214 — Re: Reuse of argument objects

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-04-24 09:50 -0400
SubjectRe: Reuse of argument objects
Message-ID<ljb4r9$hvg$1@dont-email.me>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web