When passing a variable to a method through arguments, its value is transferred, that is, a new instance is created. Changing this instance does not affect the variable that was passed.

Is it possible to pass a reference to a variable (so that a change in a variable received through the arguments entails a change in the variable passed in through the arguments) and, if so, how?


Example:

class Main { int a = 0; public static void main(String args[]) { myMethod(a); } void myMethod(int b) { b++; } } 

How to transfer a so that change b leads to change a .

  • what is the variable? -_- example - Suvitruf
  • @Suvitruf, updated the question. - user189127
  • The correct answer: no way. - Tagir Valeev

4 answers 4

Wrap her in something, for example:

 public class Container { public int myVar; } 

pass this container, function(Container c) and change the value there calmly.

But in general it is a bad practice. Everything must be immiable.

Much better if you do this:

 public int myFunction(int arg) { int result = ... //совершаем действия с arg return result; } 
  • And there are no simpler ways? ( - user189127
  • show your code, an easier way is to write correctly. - Dmitry V. Nov.
  • It's not about the code ... Apparently, there are no more ways ... - user189127
  • After 4 minutes, I will accept the answer (the site swears). - user189127
  • @ bukashka101 try to keep the functions clean (no side effects). Save yourself a lot of time later in debugging and testing. - Dmitry V. Nov.

You can, for example, use a generic generic.

 class Main { Reference<Integer> a = new Reference<>(0); public static void main(String args[]) { myMethod(a); } void myMethod(Reference<Integer> ref){ ref.set(ref.get() + 1); } } public class Reference<T> { private T referent; public Reference(T initialValue) { referent = initialValue; } public void set(T newVal) { referent = newVal; } public T get() { return referent; } } 
  • @DmitryV. not abstract - Suvitruf

In fact, @Dmitry V. already wrote about this - I don’t want to take someone else’s fame and made the answer general. I would like to expand a little.

In Java, there is no such action as passing an argument by reference; it was decided to abandon it in favor of transparency of actions and avoidance of non-obvious errors. So there is a simple rule:

In Java, all arguments are passed by value.

What to do in a situation similar to the situation in the question when you need to influence global variables from a method.
Immediately, we note that, although wrapping in containers and generics, although they solve the problem, it is unnecessarily difficult and not obvious when third-party code reads, it’s definitely not necessary to abandon it in such a solution.

Meanwhile, there is a simple "canonical" method of this action - the return value by the method:

 class Main { int a = 0; public static void main(String args[]) { a = myMethod(a); } int myMethod(int b) { return b++; } } 

In addition, if you are a Hindu at heart, then you can generally put on canons and conventions and work in the method directly from the global variable:

 class Main { int a = 0; public static void main(String args[]) { myMethod(); } void myMethod() { a++; } } 

In addition to the populated "common cultural" values ​​of the language, we also see obvious shortcomings - the method is not universal and works with only one global variable, but this is to be a Hindu.
However, it is worth noting that this sarcasm will not be appropriate when using a global variable for its intended purpose - accessibility in all methods of the class. With this use, the value of a global variable can be changed directly in the methods, especially if changing it is not the purpose of the method and the method must return a different value (the code in this question does not apply to this type of use, therefore the first option is preferable).

    If we are talking about the type of int, then pass instead of the primitive int through the wrapper Integer. All primitive types in Java are passed by value, and all objects by reference. That's all)

    • No, this is not correct. The method will create a local copy of a global variable with a scope within the method. - pavlofff
    • Are you talking about primitive? - GermanSevostyanov
    • If yes, then I agree, the primitives are passed by value and a copy of them is created with the scope of the method. - GermanSevostyanov
    • Any arguments passed. Primitives and objects. - pavlofff
    • Second, check, I think you are mistaken. - GermanSevostyanov