Actually, funkyChanger will not work because you are effectively
reassigning the argument.  Since all arguments in Java are passed by
value, the assignment is not realized by the caller.

The only ways to get argument changes surfaced to the caller are using
a typed return or passing in an argument whose state can be modified
(and not by assignment).

As discussed in this thread, one way to accomplish this for primitives
is by using the MutableXxxx types found in Apache Commons Lang.
Another common way is to using a single element array.

Thanks,

Brent.

On Fri, Jan 28, 2011 at 4:01 PM, Stephen Williams <scient...@gmail.com> wrote:
> All objects are passed as references in Java.
> All fundamental scalar types have Object wrapped versions.
> An argument that is meant to be modified just needs to be an object
> reference.
>
> So, you can simply go from:
> void funkyReader(int arg) { arg++; }
> to:
> void funkyChanger(Integer arg) { arg++; }
>
> If it needs to be threadsafe, simply synchronize on the object everywhere it
> is used.
>
> Autoboxing will not cause an 'int' variable to change as it is just copied
> to create the temporary object.
>
> Stephen
>
> On Tue, Jan 4, 2011 at 12:58 AM, Julien Aymé <julien.a...@gmail.com> wrote:
>
>> If you only use boolean, integer and long, you can also use AtomicXXX
>> from java.util.concurrent.atomic (if you have to use them in a
>> multi-threaded environment).
>> Otherwise, the MutableXXX wrappers in Commons Lang are good.
>>
>> Julien
>>
>> 2011/1/3 Paul Benedict <pbened...@apache.org>:
>> > Michael,
>> >
>> > Primitives are passed by value in Java. If you want to "modify"
>> primitives,
>> > use the MutableXXX wrappers in Commons Lang.
>> >
>> > Paul
>> >
>> > On Mon, Jan 3, 2011 at 3:01 PM, Michael Giannakopoulos <
>> miccagi...@gmail.com
>> >> wrote:
>> >
>> >> I mean exactly the same thing as C# but from the replies i see that's a
>> >> difficult task and also that has little to do with apache commons...
>> >> Because
>> >> i use java a lot it's annoying the fact that specific variables that i
>> want
>> >> to change values in other functions cannot be passed as arguments but
>> >> instead you have to declare them as arrays or other objects... Thanks
>> for
>> >> all the replies. Do you believe that something like that can start in
>> >> commons?
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
>> For additional commands, e-mail: dev-h...@commons.apache.org
>>
>>
>
>
> --
> --
> Stephen D. Williams s...@lig.net scient...@gmail.com LinkedIn:
> http://sdw.st/in
> V:650-450-UNIX (8649) V:866.SDW.UNIX V:703.371.9362 F:703.995.0407
> AIM:sdw Skype:StephenDWilliams Resume: http://sdw.st/gres
> Personal: sdw.st facebook.com/sdwlig twitter.com/scienteer
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to