I know that java does not support pass by reference. But still is there any possibility to implement it in any other ways if not directly as in C/C++?
-
1What are you actually trying to do? Whatever it is, there's probably a way to do it. – khelwood Oct 25 '18 at 08:43
-
1Java uses pass by reference for objects and pass by value for primitives, don't know if there's a way to make it explicit though. – SBylemans Oct 25 '18 at 08:43
-
4@SBylemans no. Java passes references by value. That's different from passing by reference as in C++ – JB Nizet Oct 25 '18 at 08:44
-
c doesn't have pass by reference. You can do something like `void method(ArgClass[] arg), then you can assign arg[0] = ... and it behaves a bit more like a pointer/reference. – matt Oct 25 '18 at 08:56
-
The only real way is to use holder objects or holder arrays, which is pretty cumbersome. In general, if you want/need pass by reference behavior, then you are probably doing something wrong and should reconsider your design. – Mark Rotteveel Oct 25 '18 at 09:03
-
Looks similar to https://stackoverflow.com/questions/4520137/does-java-have-mutable-types-for-integer-float-double-long – Miller Cy Chan Oct 25 '18 at 09:09
-
1"I know it can't be done but I want to do it anyway". That way lies madness and eternal tears. – jwenting Oct 25 '18 at 09:20
-
@JBNizet ok wow, I did not know it worked like that. Good to know, thanks for the info! [Explanation](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – SBylemans Oct 25 '18 at 09:37
3 Answers
Nope. You can't do it. It's not something you can "implement". It's a fundamental aspect of the language.

- 41,989
- 11
- 82
- 128
If what you're trying to do is to have a "reference" to a primitive value which you can pass around, so you can modify the primitive's value inside methods and have the changes be visible outside said methods...
... just pass an array containing the primitive, like this:
// By value
int value = 1;
changeValue(value); // changes value to 5
System.out.println(value); // prints 1
// By "reference"
int[] reference = {1};
changeValue(reference); // changes value inside "reference" to 5
System.out.println(reference[0]); // prints 5
// Methods
void changeValue(int value) { value = 5; }
void changeValue(int[] reference) { reference[0] = 5; }
Bear in mind that this is not "pass by reference". Java is not passing a reference to the value, Java is passing the value of the array's reference.
You can read Is Java "pass-by-reference" or "pass-by-value"? for a better discussion about this.

- 7,103
- 2
- 37
- 58
-
-
Glad I could help you :) Please remember to upvote / accept the answers that you find useful (mine or others') so people keep answering ^^ – walen Oct 26 '18 at 10:52
Pass-by-value was by design: f(x)
will maybe alter the internals of the value of x, but x will not get another value. _For instance you cannot assign null to x inside f, using the formal parameter of f.
There is the possibility of in-out parameters which in java can be done by the function's result:
insertInNodes
is such a method.
public class SortedLinkedList<T extends Comparable<T>> {
private static class Node { ... }
Node head;
int size;
public void insert(T data) {
head = insertInNodes(head, data);
}
private Node insertInNodes(Node node, T data) {
if (node == null || data.compareTo(node.data) < 0) {
++size;
return new Node(data, node);
}
if (data.compareTo(node.data) > 0) {
node.next = insertInNodes(node.next, data);
}
return node;
}
}
Pass-by-reference needs to either fake it: pass a container object and then the component is actually a reference. For instance an array with one element or a wrapper class like Atomic<String>
.
Pass-by-reference in a more pure form could be realized by passing a getter and setter of the wanted "variable."
public void insert(T data) {
insertInNodes(this::getHead, this::setHead, data);
// Or insertInNodes(() -> head, (n) -> head = n, data);
}
private void insertInNodes(Supplier<Node> getter, Consumer<Node> setter, T data) {
if (getter.get() == null || data.compareTo(getter.get().data) < 0) {
++size;
setter.apply(new Node(data, getter.get()));
}
if (data.compareTo(node.data) > 0) {
// insertInNodes(() -> node.next, (n) -> node.next = n, data);
// or
insertInNodes(node::setNext, node::getNext, data);
}
return node;
}
Is this usable? No, only in specific use-cases.

- 107,315
- 7
- 83
- 138