20

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)
{
...
}
Community
  • 1
  • 1
NDeveloper
  • 1,837
  • 4
  • 20
  • 34
  • 2
    I think all objects are passed by ref implicitly in java – snoofkin May 18 '11 at 12:28
  • 4
    @soul Nope. Everything is passed by value in Java including references – Sean Patrick Floyd May 18 '11 at 12:28
  • 1
    This has been asked dozens of times. One of them is this: [How to pass by reference in Java](http://stackoverflow.com/questions/5614562/how-to-pass-by-reference-in-java) – Sean Patrick Floyd May 18 '11 at 12:29
  • No by default in Java everything is passed by value ... I am interested is there a way to pass by reference. – NDeveloper May 18 '11 at 12:29
  • See my answer here for a way around it. http://stackoverflow.com/q/3997518/2598 – jjnguy May 18 '11 at 12:32
  • No, no no - Java is ONLY pass by value. Objects are NOT passed; references to objects are passed by value. If you want to pass a reference to an Object, then you have you wish. That's all you can do. – duffymo May 18 '11 at 12:32
  • Java is pass by value, BUT there is WORKAROUND of achieving otherwise. Using an array holding the reference of the object. Even though array is passed as value, so if you modify array it won't reflect. But array holds a reference to the Integer object. TRY THIS:- { Integer[] i = new Integer[] {new Integer(0)}; System.out.println(i[0]); i[0] = i[0]+1; System.out.println(i[0]); modify(i); System.out.println(i[0]); } public static void modify(Integer[] i) { i[0] = i[0]+1; } – Prateek Pande Jan 13 '21 at 13:25

5 Answers5

49

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.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
34

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 2
    You could pass in a 'holder' object which is a mutable reference. A simple implementation is an ArrayList of 1 element. Often you just want more than one output, in which case you can use a Pair or tuple types. – karmakaze May 18 '11 at 20:31
  • @karmakaze Yes, that's a workaround, but it's not possible to get real pass-by-value in Java like it exists in C# or C++. – Jesper May 18 '11 at 20:39
  • @Jesper could you elaborate on the difference between passing a reference by value and reference passing? Those seem the same to me. – kingfrito_5005 Aug 05 '15 at 14:03
  • @kingfrito_5005 Pass by reference means that a method can directly change variables that are passed from the calling method. Java doesn't support this - if you change a variable inside a method, the variable in the calling method isn't changed. A copy of the value of the variable is passed to the method - not the variable itself. – Jesper Aug 05 '15 at 18:08
5

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
3

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.

sblundy
  • 60,628
  • 22
  • 121
  • 123
1

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588