Newsgroup: it.comp.java
Titolo: How do I pass a argument ByRef?

Maybe this is very simple, but I couldn't figure a way to pass a variable by
reference.
Say you want to pass an integer to a function, and have the function modify
it: how should I declare the parameter?
TIA, GioVAX
PS: please, answer directly to me, as I am not a regular reader of this
group.

 it: how should I declare the parameter?
Use Integer rather than int.  
Like Smalltalk, Java does not allow programmer control
over reference/value storage of objects but imposes simple rule:
basic types (int, float, boolean etc) have value instances constructed 
classes have reference instances.
[C++ and Eiffel programmers have luxury of choosing val or ref
 instances for any object ]
If you want to do reference parameters generally, then write
yourself a little "Cell" class which can carry things around.
OK, OK, I'll write it for you:
  class Cell {
    public Object value;
    public Cell(Object x) {value=x;}
    }

it: how should I declare the parameter?
Use an array.
int[] foo = new int[1];
foo[0] = 2;
doit(foo);
void foo(int[] intbyref)
{
   intbyref[0] += 2;
}
jim
-----------------------------------------------------------------------
James P. White                        Netscape DevEdge Champion for IFC
Director of Technology Adventure Online Gaming 
http://www.gameworld.com
Developers of Gameworld -- Live Action Role-Playing and Strategic Games
j...@aognet.net
            Pagesmiths' home is 
http://www.pagesmiths.com

 it: how should I declare the parameter?
There is no such thing as "pass by reference" in Java. Java only
passes by value. Many newbies get confused by this because
in Java a variable of an object type is called a "reference" type. 
In actuality, a "reference" to an object is better described as
a "handle". If you are familiar with C/C++, it corresponds to passing
a pointer to an object (passing a copy of the address of the object
as a value). 
In order to do what you describe above, wrap the primitive type you
want to manipulate in a class and pass an object of that class to
the method. You can then freely manipulate the contents of the object
within the method. For example:
public class Int
{
	public int i;
}
public void bump( Int n )
{
	n.i++;
}
Don't use the java.lang.Integer or any of the other standard Java
"Wrapper" classes, they are immutable. 
BTW: This is all explained in detail in my book (see below).
-- 
Michael S. Jenkins               |   Abstract Data Types in Java
Jenkins Consulting Services, Inc |  Michael S Jenkins / McGraw Hill
jen...@pprd.abbott.com
          |        ISBN 0-07-913270-7
mjen...@jcs-inc.com
             | Get the book through my web site
http://www.wwa.com/~mjenkins
 <<<<-- in association with Amazon.com

Giovanni \"GioVAX\" Pezzino (
pez...@cres.it
) wrote:
: Maybe this is very simple, but I couldn't figure a way to pass a
: variable by reference.
It *is* very simple.  In Java, ALL reference parameters (objects) are
passed by reference.  Primitive values are passed by value.
: Say you want to pass an integer to a function, and have the function modify
: it: how should I declare the parameter?
It was suggested that you use the Integer class.  Forget it.  Integer
objects are immutable, you will be wasting your time.
Another suggestion was made to use an array.  This will work, but it is
kludgy, unclear, and unintuitive.
The proper way to do it would be to have the method (note, NOT "function")
return the integer value.  If you need more than one value modified, then
you should be using more than one method.  Assign the result of the method
call to the variable which is also in the argument list.
int val = 5;
val = myMethod (val);
// Now, val holds whatever myMethod returned.
: PS: please, answer directly to me, as I am not a regular reader of
: this group.
Well, perhaps you should be.  If you wanted me to mail you my answer, you
should have mailed me the question.  If you're going to post in a public
forum, the least you could do is extend to those taking the time to answer
the courtesy of READING their responses.
Kev.
(Posted, not mailed)
--
Kevin Swan                                                         BCSH
013...@dragon.acadiau.ca
                             Acadia University
                 How's my posting?  Call 1-800-DEV-NULL
** Fatal Error [1]: 'Win95' virus detected on /dev/hda1; Formatting ...