Reuse of argument objects

S

Stefan Ram

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?

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

Eric Sosman

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top