How to pass a value by reference to another function without creating a class or an instance of a class in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to pass a value by reference to another function without creating a class or an instance of a class in java?

I want to pass by reference and not value. but unlike c++ where we can use pointers and addresses in java we have no such symbols (&,*). Instead of creating a new class and passing it's object for reference is there any simpler way?

7th Jun 2017, 2:03 PM
Vidul Goenka
Vidul Goenka - avatar
2 Answers
+ 3
Java doesn't have pointer arithmetic as C or C++, but you can create an empty object without calling the constructor.
7th Jun 2017, 2:08 PM
Maria Zheng
Maria Zheng - avatar
+ 2
Use a method to get the value you want, setting your current variable to whatever the method returns. Note* This is still not passing by reference. Example/ int a = 5; // Now I want a new value for a a = factorial(a); // what the method looks like int factorial(int num){ if(num == 1) return 1; return num * factorial(num - 1); }
7th Jun 2017, 2:33 PM
Rrestoring faith
Rrestoring faith - avatar