Possible Duplicate:
How to pass by reference in Java
Is it possible to pass object by reference in Java
Like in C#
public static void SomeMethod(ref Object obj)
{
...
}
Possible Duplicate:
How to pass by reference in Java
Is it possible to pass object by reference in Java
Like in C#
public static void SomeMethod(ref Object obj)
{
...
}
All variables of objects are references to the object. When you pass an object to a method, you are passing the reference of the object already. If you don't want the original object to be affected, you must clone it first.
No, that is not possible in Java.
In Java, all arguments to methods are passed by value. Note that variables of non-primitive type, which are references to objects, are also passed by value: in that case, a reference is passed by value. Note that passing a reference by value is not the same as passing by reference.
No. Java is pass-by-value only.
You should not require such a thing though. You can pass the object and change its fields - this will be reflected in the caller.
You'll have to create a reference class. Fortunately, there is one. Try looking at AtomicReference
. Note that it's intended for concurrency, so it might not be suitable.
Another idea is to pass an Object array with a length of one.
Passing Reference Data Type Arguments
Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
Source: Java Tutorial > Passing Information to a Method or a Constructor