+ 2

How can we call by reference in java ?

I searched in net , call by reference is not possible in case of java . But explanation is not sufficient . Can someone provide me a code and explain that code little bit . What is the substitute for this call by reference , if not possible .

15th May 2017, 1:25 PM
cool_boy12344
cool_boy12344 - avatar
6 Answers
+ 14
You can't use pointer in Java, but you can pass reference of a non-primitive type to a void method and change it's content. Non primitive means object, array etc. Here is a sample code: https://code.sololearn.com/c613pBjuPn1q/?ref=app
15th May 2017, 2:31 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 13
@Om, okay we'll ask all people in the world, now stop trolling please. It's a help seeking post -_-
15th May 2017, 4:06 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
note* I refer to passing by reference here, but calling by reference will require the same functionality. Primitive datatypes do not and cannot be passed by reference. Instead, they are passed by value. For example void a(){ int age; b(age); } void b(int age){ age = 4; } The function from b is not changing the same variable as from function a. There is no way to pass a primitive datatype by reference in Java. Only objects are passed by reference. The usual way to deal with this are getters and setters. For example: void a(){ int age = 0; age = b(age); } int b(int age){ return age+5; } Though there is a 'hack'. You can pass arrays by reference since they're objects. So if you really wanted to you could use an array for the variable. This will take up more memory, so it's likely not as practical.
15th May 2017, 2:23 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
I might have a solution and I'm not a troll. Was just trying to have some fun. Sorry
15th May 2017, 4:07 PM
OM NITIN WAGHMODE
OM NITIN WAGHMODE - avatar
- 2
ask my friend Hayes
15th May 2017, 3:59 PM
OM NITIN WAGHMODE
OM NITIN WAGHMODE - avatar
- 4
refer to Steve Jobs for the answer
15th May 2017, 1:27 PM
OM NITIN WAGHMODE
OM NITIN WAGHMODE - avatar